Index error when filling in an array with a randomly generated array - matlab

I am getting an error when running this bit of code about the index. I have ran through the logic several times and have yet to catch my error and I am thinking it is in the way I coded this section. Any help would be greatly appreciated. Please let me know if I am missing any information vital for this bit of code.
index_pairs = [1,12661;12662,46147;46148,52362]
group_class_count = [10137,2524;127448,20738;1570,4645]
group_count = 3
cross_sections = 10
for j=1:group_count
rand_index=randsample(index_pairs(j,1):index_pairs(j,2),(group_class_count(j,1)+group_class_count(j,2)),true); % Creates an index of random rows for the current group.
cross_size(j)=floor(size(rand_index,2)/cross_sections);
for k=1:cross_sections
cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};
end
end
error: Index exceeds matrix dimensions. Error in cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};

If you change
cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};
to
cross_rand_indices(j,k)={rand_index(cross_size(j)*(k-1)+1:cross_size(j)*(k))};
the error will disappear.
I assume this is in line with your intent when saving something to cross_size(j) in the outer loop.

Related

Swift: UnsafeBufferPointer when using larger offset values in String.index(_:Int)

I have the following line of code that sets a String.Index to a certain value using the index offset function in the swift standard library
index = str.(index, offsetBy: -length)
it functions for short str, but as soon as it, or the length value get above some arbitrary (and changing!) threshold, I get a variety of crashes. Most consistently, and from a break point is
Swift/UnsafeBufferPointer.swift:913: Fatal error
I have done a little research plus my own testing and have not found anything conclusive so far. The most helpful test I ran was trying to offset this index in smaller increments with a loop such as with the following code
for _ in 0...Int(totalLength) {
let newIndex = str(index, offsetBy: -1)
index = newIndex
}
however, the let newIndex line still crashed even with the offset value at 1, so I imagine that it is the cause of the long string. However, with this test I was more consistently getting BAD ACCESS errors instead of the UnsafeBufferPointer.
I have not found anyone online who has run into a similar problem, and am wondering if this is a documented issue with the function, or if there is some other thing at fault. Any help is appreciated, thank you !

MATLAB 'Gather' on Tall Array Never Terminating

I am running R2017a on Windows 10, and using a tall array that was constructed off a Datastore object (that was, itself, constructed from tall arrays built on MATLAB matrices of doubles).
What really bugs me about the issue I'm having now is that all my code used to work fine. One day it simply started hanging when I tried to run it.
This is the block in question:
%load
tallTraindat = tall(datastore);
sz = size(tallTraindat);
sz = gather(sz);
numExamples = sz(1);
exampleLen = sz(2);
My tall array is a M x ~1700 single array, constructed on this datastore of smaller single arrays. In a loop:
write(fname,tallEx); % tall ex constructed by tall(someSingles);
folderNames{end+1} = fname;
and then:
ds = datastore(folderNames,'Type','tall');
As you can see, this is about as vanilla as it gets. But the operation 'sz = gather(sz)' simply hangs forever. It never finishes or returns. My parallel pool has started properly, and the gather operation gets to the point where it prints 'Evaluation 100% complete'. But it goes nowhere from there. If I pause execution, I'm always taken to a point in RemoteSpdmExecutor, line 129 'obj.RemoteSpmdController.drainIO( false );'. This line apparently lasts forever.
EDIT: When I woke up today, it started failing with an error message instead : 'Error using parallel.FevalOnAllFuture/fetchOutputs (line 69)
fetchOutputs could not concatenate the OutputArguments. Set 'UniformOutput' to false.
Cell contents reference from a non-cell array object.'
EDIT: Re-created my default local parallel pool a couple times. Now it's back to hanging on that same line of code.
Based on all my tests, it seems that my issue occurs whenever I construct a tall datastore from more than one tall-array folder, and then construct a tall array off of that datastore.
Tearing my hair out over this one. If anyone even has a suspicion where to look for the source of this problem, I'd appreciate it. I'll try to respond quickly to requests for more info.

Trouble with running a parallelized script from a driver script

I'm trying to parallelize my code, and I finally got the parfor loops set up such that Matlab doesn't crash every time. However, I've now got an error that I can't seem to figure out.
I have a driver script (Driver12.m) that calls the script that I'm trying to parallelize (Worker12.m). If I run Worker12.m directly, it usually finishes with no problem. However, every time I try to run it from Driver12.m, it either 1) causes Matlab to crash, or 2) throws a strange error at me. Here's some of my code:
%Driver script
run('(path name)/Worker12.m');
%Relevant worker script snippet
parfor q=1:number_of_ranges
timenumber = squeeze(new_TS(q,:,:));
timenumber_shift = circshift(timenumber, [0 1]);
for m = 1:total_working_channels
timenumberm = timenumber(m,:);
for n = 1:total_working_channels
R_P(m,n,q) = mean(timenumberm.*conj(timenumber(n,:)),2);
R_V(m,n,q) = mean(timenumberm.*conj(timenumber_shift(n,:)),2);
end
end
end
Outcome #1: "Matlab has encountered an unexpected error and needs to close."
Outcome #2: "An UndefinedFunction error was thrown on the workers for ''. This might be because the file containing '' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details. Caused by: Undefined function or variable ""."
However, if I run Worker12.m directly, it works fine. It's only when I run it from the driver script that I get issues. Obviously, this error message from Outcome #2 isn't all that useful. Any suggestions?
Edit: So I created a toy example that reproduces an error, but now both my toy example and the original code are giving me a new, 3rd error. Here's the toy example:
%Driver script
run('parpoolexample.m')
%parpoolexample.m
clear all
new_TS = rand([1000,32,400]);
[number_of_ranges,total_working_channels,~] = size(new_TS);
R_P = zeros(total_working_channels,total_working_channels,number_of_ranges);
R_V = zeros(total_working_channels,total_working_channels,number_of_ranges);
parfor q=1:number_of_ranges
timenumber = squeeze(new_TS(q,:,:));
timenumber_shift = circshift(timenumber, [0 1]);
for m = 1:total_working_channels
timenumberm = timenumber(m,:);
for n = 1:total_working_channels
R_P(m,n,q) = mean(timenumberm.*conj(timenumber(n,:)),2);
R_V(m,n,q) = mean(timenumberm.*conj(timenumber_shift(n,:)),2);
end
end
end
Outcome #3: "Index exceeds matrix dimensions (line 7)."
So, at the 'parfor' line, it's saying that I'm exceeding the matrix dimensions, even though I believe that should not be the case. Now I can't even get my original script to recreate Outcomes #1 or #2.
Don't use run with parallel language constructs like parfor and spmd. Unfortunately it doesn't work very well. Instead, use cd or addpath to let MATLAB see your script.

error in storing captured images in a structure

I want to capture 100 images from my webcam and then store them in a structure. I'm trying to do it like this but i'm getting the error, 'subscripted assignment dimensions mismatch'.
The code is this:
sep_images=struct('images',[]);
vid=videoinput('winvideo',1,'YUY2_320x240');
set(vid,'FramesPerTrigger',Inf);
set(vid,'ReturnedColorspace','rgb');
vid.FrameGrabInterval=1;
start(vid)
for num_frames= 1:100
im=getsnapshot(vid);
sep_images.images(num_frames)=im;
end
stop(vid);
and it is giving me the error in this statement, sep_images.images(num_frames)=im;.
If someone has the idea of how to do it? please let me know.
I think you intended the images field to be a cell.
Initialize like:
sep_images=struct('images',{[]})
Assign like:
sep_images.images{num_frames}=im;
Just remember to access it with curly braces too (i.e. I = sep_images.images{iframe}).

getblast error unavilable matlab

I'm writing matlab code that's supposed to iterate over a list sequences and blast each at a time. Here is the relevant part of the code:
%blast the seq
[res, ROTE] = blastncbi(seq, 'blastn');
res1 = getblast(res, 'WaitTime',ROTE);
resName = res1.Hits(1).Name
for some seq's it worked, and then for the last it gave me this error message:
Error using getblast (line 176)
BLAST V7EBUE0901R is unavailable - try later.
please note that I've defined ROTE as the 'WaitTime' value, as suggested in the documentation of this function.
The script must iterate over lots and lots of genes, so I can't let it crash every 5 minutes!
The RTOE returned by blastncbi is an estimated time of how long it takes. Perhaps the estimate is sometimes simply incorrect.
Two simple ways to deal with this could be waiting longer, or trying it twice:
res1 = getblast(res, 'WaitTime',ROTE*10);
or
try
res1 = getblast(res, 'WaitTime',ROTE);
catch
res1 = getblast(res, 'WaitTime',ROTE);
end
Of course this assumes that you are sure that the info that you request is actually available.