From: radar on
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.
From: Walter Roberson on
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 () .
From: TideMan on
On May 3, 1:02 pm, radar <lcar...(a)tds.net> 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;
>
> 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.

You need to use a cell array:
c{i}=find(mat(:,4)==i);
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.
From: radar on
thank you Walter
From: radar on
thank you Derek on both counts