how to flip a general square matrix ( it could have even or odd number of rows/cols ) from it's center in matlab ?

조회 수: 1 (최근 30일)
hi all
I have a general matrix , I want to flip the last half of it starting from it's center in matlab
To be clear ,I attach an image
I try the following
A=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25]
num_of_row=length(A(:,1))
if( mod(2,num_of_row)==0)
k=M/2;
else
k=(M-1)/2;
end
fliplr(A)
flip.jpeg

채택된 답변

Stephen23
Stephen23 2019년 1월 16일
>> M = [1,2,3,4,5;2,6,7,9,10;3,7,11,12,13;4,9,12,16,20;8,10,13,20,25]
M =
1 2 3 4 5
2 6 7 9 10
3 7 11 12 13
4 9 12 16 20
8 10 13 20 25
>> X = [5,4,1,2,3];
>> Z = M(X(end:-1:1),X)
Z =
13 12 3 7 11
10 9 2 6 7
5 4 1 2 3
20 16 4 9 12
25 20 8 10 13
  댓글 수: 4
Stephen23
Stephen23 2019년 1월 16일
편집: Stephen23 2019년 1월 16일
"but why this way doesn't work in general "
What I showed does work in general: any valid set of indices can be used to move around the rows and columns of any matrix, exactly as I showed in my answer.
For some unknown reason you tried to use the matrix data itself as indices, which is totally unrelated to anything in my answer, and totally unrelated to anything in your question.
"Subscript indices must either be real positive integers or logicals."
That error message tells us that the indes values that you used are not valid indices for that matrix. If X is supposed to be a set of indices, why are you creating this by concatenting a (whatever that is) with the first row of the matrixc data?:
X = [a,row_1(1:mid-1)];
% ^ supposed to be indices, for M(X(end:-1:1),X).
% ^ what is this?
% ^^^^^ why are you using matrix data as indices?
Unless you expect the first row to contain indices, this does not make sense. You certainly did not explain this in your question or any of your images. Please explain what you are trying to achieve.
fatema hamodi
fatema hamodi 2019년 1월 16일
yes yes you are right , I fix it ,no I don't need the matrix data it was by mistake , it works now thank you very much !!

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 15일
Perhaps?
a(:,end:-1:1) % columns
a(end:-1:1,:) % rows
  댓글 수: 5

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by