how can I shift the columns of a matrix with the for loop?

4 ビュー (過去 30 日間)
omar sivori
omar sivori 2019 年 2 月 25 日
回答済み: Yasasvi Harish Kumar 2019 年 2 月 25 日
if I have a function that outputs a new matrix which shift the input matrix columns from 1 index how can I perform this operation with a for loop?
  1 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 25 日
Illustrate with an example.

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

回答 (1 件)

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019 年 2 月 25 日
Hi,
Say your input matrix(t) is an n*m matrix.
for i = 1:n
for j = 1:m
s = t(i,j);
t(i,j) = t(i,j+1);
t(i,j+1) = s;
end
end
The above code will shift the first element of each row to the last.
The same can be performed without a loop.
s = t(:,1);
t(:,1) = [];
t(:,n) = s;
I hope it was helpful
Regards

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by