How to extract columns of a matrix using a for loop?

14 ビュー (過去 30 日間)
RG
RG 2016 年 4 月 28 日
コメント済み: Star Strider 2016 年 4 月 28 日
Hi Everyone,
I have a 3x6 matrix and trying to extract 1st and 3rd, 2nd and 5th, 3rd and 6th column using a loop. The matrix is:
polygon_nf =
0.0185 0.0192 0.0207 0.0185 0.0192 0.0207
0.0241 0.0236 0.0239 0.0241 0.0236 0.0239
0.0185 0.0192 0.0207 0.0241 0.0236 0.0239
The loop I have so far is:
for i=1:3
x_nf=polygon_nf(:,i);
y_nf=polygon_nf(:,i+3);
end
When I try to display x_nf and y_nf values from within the loop I get 6 columns that I want, however, when I display the results after the loop has finished, I only get 2 columns. And when I try to put x_nf(i) and y_nf(i) instead of x_nf and y_nf inside the loop, I get an error saying:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Any help on this issue would be highly appreciated.

採用された回答

Star Strider
Star Strider 2016 年 4 月 28 日
You need to subscript the left-side variables in your assignments as well:
for i=1:3
x_nf(:,i)=polygon_nf(:,i);
y_nf(:,i)=polygon_nf(:,i+3);
end
  4 件のコメント
RG
RG 2016 年 4 月 28 日
編集済み: RG 2016 年 4 月 28 日
Thanks for the alternative version.
The reason I thought of using loop was that in my code I want to run it more than three times (the input matrix is 3 x n), but again, thanks for the suggestion, it is highly appreciated.
Star Strider
Star Strider 2016 年 4 月 28 日
My pleasure.

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

その他の回答 (0 件)

カテゴリ

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