how to store new matrix on the old one

4 views (last 30 days)
I have a array Z(4,4) such as
[1:4;
5:8;
9:12;
13:16].
I would like to change the order of the matrix and store in a same name as Z with new version;this for the first iteration. for the second iteration; I want to use the previous version and do the same changes on the order to get new one.keep doing this for m iteration.
for k=1:m,
D=[];
for j=1:4,
for i=1:4,
C=A*[i;j];
[x]=mod(C(1),4)+1
[y]=mod(C(2),4)+1
D=[D;Z(x,y)];
end;
E=[E,D];D=[];
end;
NewZ(k)=E;E=[];
Z=NewZ;
end;
always I get this error: Subscripted assignment dimension mismatch.
Please I need a help.
  6 Comments
dpb
dpb on 25 May 2014
Don't know if it will produce what you expect or not, but the error comes from
NewZ(k)=E;
where you're trying to store an array in an element. Perhaps what you're looking for is
NewZ=[NewZ;E]; E=[];
to concatenate the E onto NewZ
sundus
sundus on 25 May 2014
NewZ is a new version of Z ,and I need to keep it aside by itself. at the end I will have m numbers of NewZ. Yes, I'll have an array but I don't know how to save it under the same name and reuse it.

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 25 May 2014
To accomplish what you appear to have in mind you would need to modify your code to:
for k=1:m,
E=[];
for j=1:4,
D=[];
for i=1:4,
C=A*[i;j];
[x]=mod(C(1),4)+1;
[y]=mod(C(2),4)+1;
D=[D;Z(x,y)];
end;
E=[E,D];
end;
Z=E;
end
There are more efficient ways of carrying out this procedure. For example, you could pre-calculate all sixteen x,y pairs (which don't depend on Z) and then carry out the m transformations more quickly.
  2 Comments
sundus
sundus on 25 May 2014
Thank you a good suggestion, you mean for k=1:m, for j=1:4, for i=1:4, C=A*[i;j]; [x]=mod(C(1),4)+1; [y]=mod(C(2),4)+1; Tem=[Tem [x,y]]; end; end; Tem=[]; end;
so I need m times from the Tem vector and I don't know how to write it.Could you please help me with that.
Really I like your efficient way to carry out the procedure.
sundus
sundus on 26 May 2014
I will have different orders for the items of Z with each iteration, I need to store each z in an array because I need them for advance process,but I don't know how to do that. Please help.
In this solution I will not have all the copies of Z, so the problem is how to store all the copies in an array.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!