Trying to make a "snake" matrix?

9 ビュー (過去 30 日間)
Tatiana Gospe
Tatiana Gospe 2016 年 10 月 10 日
回答済み: Thorsten 2016 年 10 月 10 日
N=5;
M=9;
T=zeros(N,M);
count=0;
for i=1:N
for j=1:M
count=count+1;
T(i,j)=count;
if mod(i,2)==0
T(i,j)=fliplr(T(i,j));
end
end
end
T
I'm trying to get the even rows to go 1,2,3,4 and so on to the last column, and then on the even rows I want the elements to flip from left to right. SO far I've got a matrix that goes from 1 to the last element, and I can't seem to get the even rows to flip. Thanks!
  2 件のコメント
David Goodmanson
David Goodmanson 2016 年 10 月 10 日
編集済み: David Goodmanson 2016 年 10 月 10 日
Hello TG, 'fliprl' is not working because you are applying it to individual matrix elements T(i,j). Since T(i,j) is just a number, fliprl gives back that same number and doesn't do anything. You need to apply fliprl to an entire row.
Tatiana Gospe
Tatiana Gospe 2016 年 10 月 10 日
Thanks!!

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

採用された回答

Thorsten
Thorsten 2016 年 10 月 10 日
N = 5; M = 9;
T = reshape(1:M*N, [M, N])';
T(2:2:end,:) = fliplr(T(2:2:end,:));

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2016 年 10 月 10 日
N=5;
M=9;
T = reshape(1:N*M,N,[]);
T(:,2:2:end) = T(end:-1:1,2:2:end);
or
T = reshape(1:N*M,M,[])';
T(2:2:end,:) = T(2:2:end,end:-1:1);

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by