Info

This question is closed. Reopen it to edit or answer.

matrix convertion and reconvertion

1 view (last 30 days)
Raza Ali
Raza Ali on 2 Mar 2014
Closed: Azzi Abdelmalek on 2 Mar 2014
I want to move the matrix elements as desired like
a=[1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16]
now i want to make a into b
b=[3 4 8 7;
11 12 16 15;
14 13 9 10;
6 5 1 2]
how can i do it in MATLAB and how can i convert b back into a?

Answers (2)

David Young
David Young on 2 Mar 2014
To convert a into b, you can write
b(1,1) = a(1,3);
b(2,1) = a(3,3);
...
and so on. To convert back, use
a(1,3) = b(1,1);
a(3,3) = b(2,1);
...
and so on.
I suspect that perhaps you want something more concise. If so, please explain the rule that you are using, as I can't reliably infer it from the example.

Azzi Abdelmalek
Azzi Abdelmalek on 2 Mar 2014
Edited: Azzi Abdelmalek on 2 Mar 2014
a=[1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16]
b=a(:,3:4);
c=a(:,1:2);
b(2:2:end,:)=fliplr(b(2:2:end,:));
c=flipud(c);
c(1:2:end,:)=fliplr(c(1:2:end,:));
d=[b' c'];
out=reshape(d(:),size(a,2),[])'

Community Treasure Hunt

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

Start Hunting!