for loop query

1 回表示 (過去 30 日間)
Bathrinath
Bathrinath 2012 年 2 月 23 日
編集済み: chaocanha 2013 年 10 月 23 日
Hi,
m = 3; c = [40,39,36,30,22,21]
I have to get the answer in matrixformat d= [40 39 36 ; 21 22 30] as my machine size m = 3
once the first row is over(3 elements are printed),remaining elements of c matrix has to be printed in reverse order as shown in second row of d matrix.
Advice me regarding

回答 (1 件)

Jarrod Rivituso
Jarrod Rivituso 2012 年 2 月 23 日
Reshape and fliplr will help you here:
m = 3;
c = [40,39,36,30,22,21,1,2,3,4,5,6]
result = reshape(c,m,[])'
result(2:2:end,:) = fliplr(result(2:2:end,:))
Note that there are some subtleties with how matrices are reshaped that are a result of the column-major nature of MATLAB. This is why the transpose is there. Read the reshape doc for more info
doc reshape
  1 件のコメント
Bathrinath
Bathrinath 2012 年 2 月 23 日
Thank you,it works.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by