For Programmers: Free Programming Magazines  


Home > Archive > Matlab > September 2006 > Indexing structures









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Indexing structures
Mariya Popova

2006-09-29, 8:06 am

Hallo,

Suppose we have:
gg = [10 20]; hh = [1 2 2];

Than I am doing:
ff = gg(1,hh).
The result is: ff = [10 20 20].

Than I am making:

for i = 1 : length(ff)
S(i,1).ff = ff(1,i);
end

Question: How I can avoid the loop for and directly do the indexing
to S.ff?

Thanks everybody
Titus Edelhofer

2006-09-29, 8:06 am


Hi,
you could use
ffcell = mat2cell(ff,1,ones(1,3));
[S.ff] = ffcell{:};

although in my opinion the loop is here better (easier to read).
Only remember to preallocate the structure S ...

Titus

"Mariya Popova" <Mariya.Popova@tno.nl> schrieb im Newsbeitrag
news:ef42761.-1@webcrossing.raydaftYaTP...
> Hallo,
>
> Suppose we have:
> gg = [10 20]; hh = [1 2 2];
>
> Than I am doing:
> ff = gg(1,hh).
> The result is: ff = [10 20 20].
>
> Than I am making:
>
> for i = 1 : length(ff)
> S(i,1).ff = ff(1,i);
> end
>
> Question: How I can avoid the loop for and directly do the indexing
> to S.ff?
>
> Thanks everybody



Mariya Popova

2006-09-29, 8:06 am

Thanks Titus, it's working! I will test it on large amount of data
and if the simulation time is less than the loop, I'll use cells.

Titus Edelhofer wrote:
>
>
>
> Hi,
> you could use
> ffcell = mat2cell(ff,1,ones(1,3));
> [S.ff] = ffcell{:};
>
> although in my opinion the loop is here better (easier to read).
> Only remember to preallocate the structure S ...
>
> Titus
>
> "Mariya Popova" <Mariya.Popova@tno.nl> schrieb im Newsbeitrag
> news:ef42761.-1@webcrossing.raydaftYaTP...
> indexing
>
>
>

Titus Edelhofer

2006-09-29, 8:06 am

Hi,
your welcome!
but again: the for loop needs only one enhancement, the preallocation:

S(length(ff)).ff = 0;

such that S does not grow in each step of the loop.

Titus

"Mariya Popova" <Mariya.Popova@tno.nl> schrieb im Newsbeitrag
news:ef42761.1@webcrossing.raydaftYaTP...[color=darkred]
> Thanks Titus, it's working! I will test it on large amount of data
> and if the simulation time is less than the loop, I'll use cells.
>
> Titus Edelhofer wrote:


French Caro

2006-09-29, 8:06 am

Hi,
And why not :
S = struct('ff',num2cell(ff))
?
and S' if you really want S to be n*1 instead of 1*n.

BTW in your loop as ff is a vector you don't need ff(1,i) but just
f(i).
Caroline
Mariya Popova

2006-09-29, 8:06 am

Hi Caroline,

It's also working. The problem, that I faced with your proposal is
that a structure S with field ff is determined, but it deletes the
other fields of S (fields, that exist before).

Mariya

French Caro wrote:
>
>
> Hi,
> And why not :
> S = struct('ff',num2cell(ff))
> ?
> and S' if you really want S to be n*1 instead of 1*n.
>
> BTW in your loop as ff is a vector you don't need ff(1,i) but just
> f(i).
> Caroline

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com