hi
can enyone help me to write following code lines with use of for loops
p & ommega & v matrices are as follow:
v=zeros(6,3);
for i=1:3
v(4:end,i)=1+2*i;
end
ommega=zeros(6,3);
for i=1:3
ommega(4:end,i)=75+25*i;
end
p=zeros(6,3);
for i=1:3
p(4:end,i)=0.1*i;
end

 採用された回答

Stephen23
Stephen23 2019 年 7 月 5 日
編集済み: Stephen23 2019 年 7 月 5 日

1 投票

Without loops:
>> [X,Y,Z] = ndgrid(1:3);
>> A = permute(cat(3,p(:,Z),ommega(:,Y),v(:,X)),[1,3,2]);
And checking with some examples:
>> [p(:,1),ommega(:,1),v(:,1)]
ans =
0 0 0
0 0 0
0 0 0
0.1 100 3
0.1 100 3
0.1 100 3
>> A(:,:,1)
ans =
0 0 0
0 0 0
0 0 0
0.1 100 3
0.1 100 3
0.1 100 3
>> [p(:,1),ommega(:,3),v(:,2)]
ans =
0 0 0
0 0 0
0 0 0
0.1 150 5
0.1 150 5
0.1 150 5
>> A(:,:,8)
ans =
0 0 0
0 0 0
0 0 0
0.1 150 5
0.1 150 5
0.1 150 5
>> [p(:,3),ommega(:,3),v(:,1)]
ans =
0 0 0
0 0 0
0 0 0
0.3 150 3
0.3 150 3
0.3 150 3
>> A(:,:,25)
ans =
0 0 0
0 0 0
0 0 0
0.3 150 3
0.3 150 3
0.3 150 3

2 件のコメント

ali alipour
ali alipour 2019 年 7 月 5 日
thank you very much
my problem was solved
but can do this with loops?
Stephen23
Stephen23 2019 年 7 月 5 日
編集済み: Stephen23 2019 年 7 月 5 日
"but can do this with loops?"
Of course: preallocate A and then use indexing. You could generate the indices before the loop (e.g. using ndgrid) or inside the loop using division and modulo operations.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2019 年 7 月 5 日

編集済み:

2019 年 7 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by