From: Stefano on 23 Jun 2010 04:32 Hello, as the title on the post says, I have a struct S which the type of one of its fields is double ('value'): I am wondering how I could divide all of the elements stored in S.value by a scalar, call it 'max'. The only thing I could do is getting back an array of the elements: values = [S.value]/max; That did what I needed, but I would like to be more elegant, and keeping the data in S by not using the array values.. i would say S.value = S.value/max; but this does not work. I wouldn't like to use a for in order to divide each S(i).value by max, but the idea is just that. Thanks, Stefano
From: us on 23 Jun 2010 07:01 "Stefano " <cillino25(a)yahoo.it> wrote in message <hvsgq3$foq$1(a)fred.mathworks.com>... > Hello, > as the title on the post says, I have a struct S which the type of one of its fields is double ('value'): I am wondering how I could divide all of the elements stored in S.value by a scalar, call it 'max'. > The only thing I could do is getting back an array of the elements: > > values = [S.value]/max; > > That did what I needed, but I would like to be more elegant, and keeping the data in S by not using the array values.. i would say > > S.value = S.value/max; > > but this does not work. > > I wouldn't like to use a for in order to divide each S(i).value by max, but the idea is just that. > > Thanks, > Stefano 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
From: Stefano on 23 Jun 2010 08:07 > 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
From: Steven Lord on 23 Jun 2010 10:07 "Stefano " <cillino25(a)yahoo.it> wrote in message news:hvstd8$dej$1(a)fred.mathworks.com... *snip* > 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. 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. z = mymax; for k = 1:n s(k).value = s(k).value/z; end -- 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: Stefano on 23 Jun 2010 10:22
> 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! |