Indexing with Find into cell array or matrix

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

 Respuesta aceptada

Matt J
Matt J el 6 de Jun. de 2013
Editada: Matt J el 6 de Jun. de 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

Más respuestas (1)

Matt J
Matt J el 6 de Jun. de 2013
Editada: Matt J el 6 de Jun. de 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 comentarios

Nathan
Nathan el 6 de Jun. de 2013
Well I think I'm fine with the for loop. I am just having an issue with yyy_index outputting in the format I want it to.
For example all cell arrays involved are 31x1 in size. I want the yyy_index to output exactly like that. However, its ouputting as a single matrix based on indicies of just of one of the cells in the subdewfinal array, instead of a 31x1 cell array of matricies that are based on each cell in subdewfinal.
Matt J
Matt J el 6 de Jun. de 2013
Editada: Matt J el 6 de Jun. de 2013
But you said you're fine with looping. Since each fixed iteration of the loop is meant to process only one of the 31 cells of all relevant arrays, then why, within that loop iteration, do you need yyy_index to derive from all 31 cells?
Nathan
Nathan el 6 de Jun. de 2013
I thought for some reason yyy_index was saving previous indicies because I keep getting the "index exceeds matrix dimensions" error even though the cell array dimensions are identical and each cell dimension is identical wihtin subdewfinal and subtempfinal5. So i'm trying to figure out why I keep getting the matrix dimensions error.
Matt J
Matt J el 6 de Jun. de 2013
Editada: Matt J el 6 de Jun. de 2013
So i'm trying to figure out why I keep getting the matrix dimensions error.
That should be pretty straightforward. The error message will tell you the variable whose dimensions you are exceeding (or at least the line number where it occurs). Inspect the dimensions of that variable and compare them to the values of the indices being fed into it...
Nathan
Nathan el 10 de Jun. de 2013
Well I know what the error means. But like I said each cell is completely identical in size so for cell {1,1} the matrix is 1052x1 for both subdewfinal and subtempfinal.For cell {1,2} the matrix is 960x1 for the same two cell arrays. This continues throughout both cell arrays. 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. So for example, let's say the indicies for {1,1} produce values of [545,546,....987,988,989]=yyy_index. The program is then trying to use those indicies on the next cell, {1,2} which is smaller than {1,1}.
Matt J
Matt J el 10 de Jun. de 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 el 10 de Jun. de 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.

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 6 de Jun. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by