flip a matrix using loops

hi,
im tring to flip a matrix usind withot the flip function
for ex:
[1 3 -2; -4 4 8; 7 -1 3] will cange to [7 -1 3; -4 4 8; 1 3 -2]
my script is
outFlip(i,:)= inMatrix(size(inMatrix,1)+1-i,:);

回答 (2 件)

Jan
Jan 2018 年 12 月 9 日
編集済み: Jan 2018 年 12 月 9 日

1 投票

Your line is correct already. Only the loop is missing:
n = size(matrix, 1);
for k = 1:n
outFlip(k,:) = inMatrix(n - k + 1, :);
end
Without the loop:
n = size(matrix, 1);
outFlip = inMatrix(n:-1:1, :);

1 件のコメント

Stephen23
Stephen23 2018 年 12 月 9 日
+1 simple without the loop.

サインインしてコメントする。

madhan ravi
madhan ravi 2018 年 12 月 9 日

0 投票

matrix=randi([0,4],2);
outFlip=zeros(size(matrix,1),size(matrix,2));
for i= 1:size(matrix,1)
for j=1:size(matrix,2)
outFlip(j,i)=matrix(i,j); % just swap the indices
end
end

2 件のコメント

Ariela Glikman
Ariela Glikman 2018 年 12 月 9 日
no, i mean:
[1 3 -2; -4 4 8; 7 -1 3] will cange to [7 -1 3; -4 4 8; 1 3 -2]
madhan ravi
madhan ravi 2018 年 12 月 9 日
ok then:
matrix=[1 3 -2; -4 4 8; 7 -1 3];
ctr=1;
outFlip=zeros(size(matrix,1),size(matrix,2));
for i=size(matrix,1):-1:1
outFlip(i,:)=matrix(ctr,:); % just swap the indices
ctr=ctr+1;
end
outFlip

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

2018 年 12 月 9 日

コメント済み:

2018 年 12 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by