Assigning a variable for matrices

4 views (last 30 days)
Damith
Damith on 20 May 2014
Edited: Matt J on 20 May 2014
Hi,
I need to repeat the following calculation for S1, S2 upto S39 by changing the name S (1,2,...39). How can I assign a variable for S1, S2..S39 in the following code.
JanS1TRMM=mean(S1(1:31,:)); FebS1TRMM=mean(S1(32:59,:)); MarS1TRMM=mean(S1(60:90,:)); AprS1TRMM=mean(S1(91:120,:)); MayS1TRMM=mean(S1(121:151,:)); JunS1TRMM=mean(S1(152:181,:)); JulS1TRMM=mean(S1(182:212,:)); AugS1TRMM=mean(S1(213:243,:)); SepS1TRMM=mean(S1(244:273,:)); OctS1TRMM=mean(S1(274:304,:)); NovS1TRMM=mean(S1(305:334,:)); DecS1TRMM=mean(S1(335:365,:));
Any help is appreciated.
Thanks.

Accepted Answer

Matt J
Matt J on 20 May 2014
Edited: Matt J on 20 May 2014
The recommendable thing is to get rid of the separate matrices S1...S39, replacing them instead by a single 3D array S(:,:,i).
This can be done as follows
for i=1:39
S(:,:,i)=eval(['S' num2str(i)]);
end
Now, do manipulations in terms of S, as we discussed in your previous related thread.
  2 Comments
Damith
Damith on 20 May 2014
Edited: Matt J on 20 May 2014
Thanks again Matt.
Based on our previous thread. Now I have this situation. But I am receiving an error for the following loop. S1_Monthly is 12 x 13 matrix.
Error:
Attempted to access S1_Monthly(13,:); index out of bounds because size(S1_Monthly)=[12,13].
for i=1:12
Trans1(:,:,i)=DamithCount(S1_Monthly(i,:),S1_Monthly(i+1,:));
end
Ay help is appreciated.
Thanks.
Matt J
Matt J on 20 May 2014
Edited: Matt J on 20 May 2014
If S1_Monthly is 12x13, you cannot access S1_Monthly(13,:). That row does not exist.

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 20 May 2014
S = eval(strcat('{',sprintf('S%d;',1:39),'}'));
Sn = cat(3,S{:});
ii = [31 28 31 30 31 30 31 31 30 31 30 31];
i1 = zeros(365,1);
i1(cumsum(ii) - ii + 1) = 1;
i2 = cumsum(i1);
[x,y,z] = ndgrid(i2,1:3,1:39);
Strmm = accumarray([x(:),y(:),z(:)],Sn(:),[],@mean);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!