Indexing with Find into cell array or matrix

1 view (last 30 days)
I'm having an issue with finding a way to index a cell array with the ouput also being a cell array.
for i=1:length(dew_array)
i;
%yyy_index=find(subdewfinal{i}<=dew_finalplus(i) & subdewfinal{i}>=dew_finalminus(i));
yyy_index=find(subdewfinal{i}<=.7493 & subdewfinal{i}>=-1.2507);
DewIV=subdewfinal{i}(yyy_index);
TemperI5=subtempfinal5{i}(yyy_index+1);
I want to index subdewfinal by finding the locations of values where the value is in between two values based on other cell arrays (dew_finalplus and dewfinalminus) eventually. Right now its commented out of this section of code and two hard values are substituted. I want this to output yyy_index as a cell array the same size as subtempfinal, dewfinalplus, and dewfinalminus. Then use this cell array to find values of another cell array subtempfinal which is also of the same dimensions.
As it stands yyy_index only outputs as a hard matrix of indicies for 1 cell of subdewfinal. I tried yyy_index{i} which outputs an error "Cell contents assignment to a non-cell array object." If I try to output it as a matrix with yyy_index(i) I get the error "In an assignment A(I) = B, the number of elements in B and I must be the same."
Any insight or solutions would be appreciated!
Thanks

Accepted Answer

Matt J
Matt J on 6 Jun 2013
Edited: Matt J on 6 Jun 2013
Maybe this is what you want? You want to save the yyy_index generated for each i=1...31?
yyy_index=cell(31,1);
for i=1:length(dew_array)
yyy_index{i}=find(subdewfinal{i}<=dew_finalplus{i} & subdewfinal{i}>=dew_finalminus{i});
DewIV=subdewfinal{i}(yyy_index{i});
TemperI5=subtempfinal5{i}(yyy_index{i}+1);
end

More Answers (1)

Matt J
Matt J on 6 Jun 2013
Edited: Matt J on 6 Jun 2013
I want to index subdewfinal by finding the locations of values where the value is in between two values based on other cell arrays (dew_finalplus and dewfinalminus) eventually.
That's almost possible with the code you have now, except you need to be indexing dew_finalplus/minus with braces
yyy_index=find(subdewfinal{i}<=dew_finalplus{i} & subdewfinal{i}>=dew_finalminus{i});
I want this to output yyy_index as a cell array the same size as subtempfinal, dewfinalplus, and dewfinalminus. Then use this cell array to find values of another cell array subtempfinal which is also of the same dimensions.
So as to eliminate the for-loop syntax? There are probably ways to do this with CELLFUN, but it would serve no purpose. The code will be ugly and the speed will be the same. There is no way to accelerate cell-to-cell operations using vectorization and usually little reason to. Cell arrays are usually small enough in size that for-loops run pretty fast. If the cell arrays are large, you are scattering a lot of data discontiguously in RAM and should probably be using numeric arrays instead.
  7 Comments
Matt J
Matt J on 10 Jun 2013
So what's happening is, yyy_index is holding the indicies from the previous cell array and trying to use them for the next cell array which is why it's giving me the error, and my main problem.
There's no reason yyy_index would do that in the code you've shown. It gets overwritten completely with every new pass through the loop. If you're trying to save the yyy_index generated in each pass through the loop, see my other Answer.
Nathan
Nathan on 10 Jun 2013
I figured it out. Thanks for all your help. I just needed to look at my numbers a little closer and step back and look at it simply. When I added 1 to the yyy_index it was giving me that dimension error because it was adding one to the highest index. Thanks. I'll accept your other answer because that would probably help people more in the future.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!