Attempting to rearrange a 3x3 "magic" matrix

[EDIT: Sat Jun 25 00:40:09 UTC 2011 - Reformat - MKF]
Hello, here is the code that I am currently attempting to get to take the given matrix and move the rows up 1 to get the top row on the bottom and each row moved up one spot, so the 2nd row is on the top, and the last row is now in the middle. When I attempt to use the code provided for example 2.3, I get the top row and the last row to swap, but of course that is not what the question is asking, what am I doing wrong? I can fix the problem, but I will have to not follow the directions to get it done. Thanks...
clc
clear
matr = magic(3)
Temp1 = matr(1,:);
matr(1,:) = matr(3,:);
matr(3,:) = Temp1;
Temp1 = matr(2,:);
matr

 채택된 답변

bym
bym 2011년 6월 25일

0 개 추천

try
doc circshift

댓글 수: 3

Jared Singleton
Jared Singleton 2011년 6월 25일
Thanks!!! that did exactly what I wanted it to do, but just for fun is there any way to use the Temp1, store feature to do the same move?,
Jared
Paulo Silva
Paulo Silva 2011년 6월 25일
it's just one simple line of code and there's no need to use additional memory.
bym
bym 2011년 6월 25일
not sure I understand your question, perhaps Paulo's answer suffices?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Paulo Silva
Paulo Silva 2011년 6월 25일

0 개 추천

m=magic(3);
m=[m(2:end,:);m(1,:)]; %works for any magic array size

댓글 수: 7

Jared Singleton
Jared Singleton 2011년 6월 25일
The script is
Matr = [1:9; 10:18; 19:27; 28:36; 37:45; ...
46:54; 55:63; 64:72; 73:81];
Temp1 = Matr(1:3, 1:3);
Matr(1:3, 1:3) = Matr(7:9, 7:9);
Matr(7:9, 7:9) = Temp1;
Temp1 = Matr(1:3, 7:9);
Matr(1:3, 7:9) = Matr(7:9, 1:3);
Matr(7:9, 1:3) = Temp1;
Matr
This is the setup for an example in the book that the question they posed later referred to. The question asked me to rearrange a magic(3) matrix using the script from the example (above). The issue I was running into was I think the matrix is too small to use the above scripts to complete the question.
Paulo Silva
Paulo Silva 2011년 6월 25일
that array got size 9*9, it can hold 9 magic arrays of size 3*3, now think that the array 3*3 can hold 9 elements of size 1*1
Matr=magic(3)
Temp1 = Matr(1, 1);
Matr(1, 1) = Matr(3, 3);
Matr(3, 3) = Temp1;
Temp1 = Matr(1, 3);
Matr(1, 3) = Matr(3, 1);
Matr(3, 1) = Temp1;
Matr
Paulo Silva
Paulo Silva 2011년 6월 25일
1:3 becomes just 1
7:9 becomes just 3
4:6 becomes just 2 (it's not used in that code)
Jared Singleton
Jared Singleton 2011년 6월 25일
ok that makes sense, but I tried using matr(1,:) to just move the rows around rather than the exact elements, using that setup, and I got horribly lost on it.
Paulo Silva
Paulo Silva 2011년 6월 25일
I'm also lost, you insist on using that script just to shift up the rows?
Temp1=matr(3,:) %save what's inside last row
matr(3,:)=matr(1,:) %replace what's inside last row by the first row
Jared Singleton
Jared Singleton 2011년 6월 25일
Not if i don't have to, the script that they reference us to use as a guide to shift entire rows and columns doesn't work well for the magic(3). the circshift works for shifting td the rows down. I am just wondering if it is even possible to use the example script to answer the question at all. The question is that we have to create a matrix based off of the magic(3) in which each row of magic(3) has been moved up by one row and the first row becomes the last row. At the start it states that we have to use the element swapping technique in the earlier example (which I posted above). I personally think it can be done with less processing power used, and with a lot simpler code than the example.
Paulo Silva
Paulo Silva 2011년 6월 25일
The code I provided in my first answer works the way you need, you can also use the circshift like this m=circshift(m,-1); and get the same results.

댓글을 달려면 로그인하십시오.

Matt Fig
Matt Fig 2011년 6월 25일

0 개 추천

If it must be done using storage techniques (as you seem to be suggesting), simply work your way down the matrix.
M = magic(3);
TMP = M(1,:);
M(1,:) = M(2,:);
M(2,:) = TMP;
TMP = M(2,:);
M(2,:) = M(3,:);
M(3,:) = TMP
Or, for a more general approach:
for ii = 1:size(M,1)-1
TMP = M(ii,:);
M(ii,:) = M(ii+1,:);
M(ii+1,:) = TMP;
end
Of course it goes without saying that this is not really the way to do it in MATLAB. Much preferred is (for one):
M = M([2:size(M,1), 1],:)

댓글 수: 1

Jared Singleton
Jared Singleton 2011년 6월 25일
matr = magic(3)
A = circshift(matr,2) %shifts each row up 1
B = circshift(matr,[0,1])%shifts each column to the right by 1
This also works from what I can tell, note to self.. stop over thinking the script and just write it. Thanks to all of you gentlemen for you assistance... I greatly appreciate it.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Functions에 대해 자세히 알아보기

질문:

2011년 6월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by