how to change variable name in for loop

I am trying to do a problem for structural analysis, i got stuck in this part trying to make a for loop give me different variables, here's the example. Thanks in advance!!
for i=1:nel
kl =[J(1,i) 0 0 -J(1,i) 0 0
0 J(3,i) J(2,i) 0 -J(3,i) J(2,i)
0 J(2,i) J(4,i) 0 -J(2,i) J(5,i)
-J(1,i) 0 0 J(1,i) 0 0
0 -J(3,i) -J(2,i) 0 J(3,i) -J(2,i)
0 J(2,i) J(5,i) 0 -J(2,i) J(4,i)]
end
I need a new "kl" each iteration, can anybody help me out, it's my first month using matlab.

1 件のコメント

Jan
Jan 2014 年 10 月 11 日
編集済み: Jan 2014 年 10 月 11 日
What is the problem with the shown code? You need a new kl in each itereation and you get a new one. So what is your question? What is the connection to the title "change varaible name"?

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

 採用された回答

Jan
Jan 2014 年 10 月 11 日

1 投票

A bold guess: Do you want to store the different matrices in a variable?
kl = zeros(6, 6, nel)
for i = 1:nel
kl(:,:,i) =[J(1,i) 0 0 -J(1,i) 0 0; ...
0 J(3,i) J(2,i) 0 -J(3,i) J(2,i); ...
0 J(2,i) J(4,i) 0 -J(2,i) J(5,i); ...
-J(1,i) 0 0 J(1,i) 0 0; ...
0 -J(3,i) -J(2,i) 0 J(3,i) -J(2,i); ...
0 J(2,i) J(5,i) 0 -J(2,i) J(4,i)];
end
Or in a cell array:
kl = cell(1, nel);
for i = 1:nel
kl{i} = ...

3 件のコメント

Oleg Komarov
Oleg Komarov 2014 年 10 月 11 日
Alejandro's comment moved here:
Thanks a lot, the problem was the one you boldly guessed. My only remaining question would be if there's a way of naming the matrices kl1, kl2, kl3, kl4, kln instead of kl(:,:,1) etc?
Oleg Komarov
Oleg Komarov 2014 年 10 月 11 日
@Alejandro: absolutely avoid this programming practice. Keep the data in one place and if you need the meta info associated withe the name of the slice, then use structures or tables.
Jan
Jan 2014 年 10 月 12 日
@Alejandro: Hiding an index in the name of a variable is a bad programming style.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeHistorical Contests についてさらに検索

質問済み:

2014 年 10 月 11 日

コメント済み:

Jan
2014 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by