collecting data in loops

  • Follow


How do I store results generated in a loop when they are of different lengths?

for i=1:390
    find(mat(:,4)==i);
end;

I'm searching a matrix for values in the 4th column. 
Because of the data therein, find will return a different number of indices for each value of i 
(find i==1 returns 100 indicies, i == 2 returns 75, etc)

I need to store those results as they are collected, and refer back to them by the value of i as an index.

I'm a newbie and would appreciate your help. Thanks.
0
Reply lcaruso (10) 5/2/2010 9:02:10 PM

radar wrote:
> How do I store results generated in a loop when they are of different lengths?
> 
> for i=1:390
>     find(mat(:,4)==i);
> end;

Use cell arrays.

foundindices{i} = find(mat(:,4)) == i;

Noitice the {} instead of () .
0
Reply Walter 5/3/2010 2:03:14 AM


On May 3, 1:02=A0pm, radar <lcar...@tds.net> wrote:
> How do I store results generated in a loop when they are of different len=
gths?
>
> for i=3D1:390
> =A0 =A0 find(mat(:,4)=3D=3Di);
> end;
>
> I'm searching a matrix for values in the 4th column.
> Because of the data therein, find will return a different number of indic=
es for each value of i
> (find i=3D=3D1 returns 100 indicies, i =3D=3D 2 returns 75, etc)
>
> I need to store those results as they are collected, and refer back to th=
em by the value of i as an index.
>
> I'm a newbie and would appreciate your help. Thanks.

You need to use a cell array:
c{i}=3Dfind(mat(:,4)=3D=3Di);
Note the curly brackets.

BTW, some gratuitous advice:
Don't use i or j as indices.
By default, Matlab treats them as sqrt(-1).
Use symbols like k, m, n, ia, etc.
0
Reply TideMan 5/3/2010 2:04:59 AM

2 Replies
333 Views

(page loaded in 0.047 seconds)

Similiar Articles:













7/22/2012 5:16:48 AM


Reply: