How do I loop a matrix with multiple changing variables ?

9 ビュー (過去 30 日間)
Mvdw
Mvdw 2016 年 5 月 2 日
編集済み: Stephen23 2016 年 5 月 2 日
I'm trying to fill in a matrix with 3 changing variables.
Example: I've got a dataset M, with 10 different values in each colon. So I say:
A= M(:,10)
B= M(:,11)
C= M(:,12)
I'm trying to fill these in into this matrix:
if true
% code
R = [A A+B A+C;
B+A B B+C;
C+A C+B C]
end
So for the first value A=1, B=2 and C=3 the matrix should look like:
if true
% code
[1 3 4;
3 2 5;
4 5 3]
end
How can I loop this so it will give me 10 matrices?
  1 件のコメント
Stephen23
Stephen23 2016 年 5 月 2 日
編集済み: Stephen23 2016 年 5 月 2 日
Adam's answer is the way to go. Do NOT try to create or access multiple variable in a loop, this makes for very buggy and slow code:

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

回答 (1 件)

Adam
Adam 2016 年 5 月 2 日
R = [A; A+B; A+C; B+A; B; B+C; C+A; C+B; C];
will give you the 10 matrices as columns of R which can be reshaped to 3*3 if required. e.g.
R = reshape( R, [3 3 10] );
then R(:,:,1) will be the first upto R(:,:,10) the 10th.

カテゴリ

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