How to fill a 3D Array using a for loop

Hi, I've been having a problem filling a matrix. I need to fill it using a "for" loop, but the equations for each element is different. I hope the example can explain better what I pretend to achieve. Hope someone can help. Thanks.
for i=1:5
k(i,1,1)=2*x(i);
k(i,1,2)=9*y(i);
k(i,2,1)=-2*x(i);
k(i,2,2)=-2*y(i);
end

 採用された回答

KSSV
KSSV 2016 年 11 月 18 日

0 投票

x = rand(5,1) ;
y = rand(5,1) ;
k = zeros(2,2,2) ;
for i=1:5
k(i,1,1)=2*x(i);
k(i,1,2)=9*y(i);
k(i,2,1)=-2*x(i);
k(i,2,2)=-2*y(i);
end
What problem you face? Code is fine.

1 件のコメント

Pedro Pires
Pedro Pires 2016 年 11 月 18 日
The weird thing is that without changing the code, it's now working fine. I can't remember exactly what the error was but I wasn't being able to give a value to the array members. Maybe some values or equations were preventing the script from working correctly. I sincerely can't see anything that I changed that could possibly make/solve the problem, but I'm also a beginners with Matlab. Thank you for the reply and help.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2016 年 11 月 17 日

コメント済み:

2016 年 11 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by