From: Steven Lord on

"Stefano " <cillino25(a)yahoo.it> wrote in message
news:hvt5ac$n4g$1(a)fred.mathworks.com...
>> So how big is n? How many elements does your struct array have? If it's
>> not VERY large, just use the straightforward FOR loop.
>
> No, n is just quite small... a hundred of elements.
> I know i could have done it with a for loop but I've asked for a more
> elegant way to do this.
> If there's not, i will go with the for!

"More elegant" often means "more complicated". You're likely going to be
able to understand instantly the FOR loop method when you look back at your
code six months from now. Will you be able to say the same about a "more
elegant" method? In my opinion, if it's fast enough and easy enough to
understand, you should just go ahead and use the FOR loop approach.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: us on
"Stefano " <cillino25(a)yahoo.it> wrote in message <hvstd8$dej$1(a)fred.mathworks.com>...
> > one of the possible solutions
> >
> > clear s; % <- save old stuff...
> > s(1).a=1:5;
> > s(2).a=11:15;
> > r=arrayfun(@(x) x.a/max(x.a),s,'uni',false);
> > r=cell2struct(r,'a');
> > r.a
> > %{
> > % r(1).a =
> > 0.2 0.4 0.6 0.8 1
> > % r(2).a =
> > 0.73333 0.8 0.86667 0.93333 1
> > %}
> >
> > us
>
> I now realized that I did not explain well my situation.. sorry, i'm quite new with structs.
>
> s.value is NOT an array, is a simple number: so what i'd like is to divide the entire field s.value (i.e. s(1).value, s(2).value, ....., s(n).value) by the same scalar.
>
> s(1).value = 1;
> s(2).value = 2;
> s(3).value = 3;
> ...(istructions that i'm asking for)
> s(1).value
> s(2).value
> s(3).value
>
> %{
> % s(1).value =
> 0.33333
> % s(2).value =
> 0.66666
> % s(3).value =
> 1
> %}
>
> I know that for having this I could have done with a simple array, but i need structs for the association between s(1).value and s(1).pos, another field of s.
>
> Stefano

oh well... if only people were THINGKING before POSTING...

one of the solutions

clear s; % <- save old stuff...
s(1).a=1;
s(2).a=2;
s(3).a=3;
n=10;
v=num2cell([s.a]./n);
[s(1:numel(s)).a]=deal(v{:});
s.a
%{
% s(1).a =
0.1
% s(2).a =
0.2
% s(3).a =
0.3
%}

needless to say that this is NOT a typical approach...
us
From: Stefano on
Maybe people that's POSTING can't THINK enough well because they're new to the subject. I said sorry, there's no need to discuss.

However, I opened this post because I'm new in structs and I'm new in Matlab as well:
i'm discovering it, and it's really powerful.. i thought that what I wanted could be done with only one easy istrunction (there's lot of stuff much more complicated than this which is resolved in only a couple of instructions).

If you tell me that there's not, ok, i'm going to do with the FOR loop.

Thanks you all.

Stefano
From: us on
"Stefano " <cillino25(a)yahoo.it> wrote in message <hvt7rg$bq6$1(a)fred.mathworks.com>...
> Maybe people that's POSTING can't THINK enough well because they're new to the subject. I said sorry, there's no need to discuss.
>
> However, I opened this post because I'm new in structs and I'm new in Matlab as well:
> i'm discovering it, and it's really powerful.. i thought that what I wanted could be done with only one easy istrunction (there's lot of stuff much more complicated than this which is resolved in only a couple of instructions).
>
> If you tell me that there's not, ok, i'm going to do with the FOR loop.
>
> Thanks you all.
>
> Stefano

wrong: the stream goes
THINK -> POST
and nothing else...
also, did you take the time to look at the last solution that was shown to you(?)...

us
From: Stefano on
As you guess I didn't...but I've just checked and it worked fine.

Thank you.. next time i'll write better my questions.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Writing structs to XML-file
Next: PortWatcher via MATLAB??