Creating variables (matrices) in for loop

Hello, I'd like to create new variables in a for loop that are matrices. It wolud be something along the lines of:
for i=1:n
ki=
[a(i) b(i) a(i)*b(i) -a(i)*b(i);.......;etc.]
So it would create variables k1,k2...kn with the first row if k1 being a(1) b(1) etc. Any way to do this? This would really help simplify my code.

 채택된 답변

Marc Jakobi
Marc Jakobi 2016년 10월 10일
편집: Marc Jakobi 2016년 10월 10일

0 개 추천

Creating new variables dynamically is bad practice. Store them in a cell array or in a struct. For example:
C = cell(n,1);
for i = 1:n
A{i} = [a(i) b(i) a(i)*b(i) -a(i)*b(i);.......;etc.];
end
This article was written for Python, but it also applies to Matlab. Here's another article on the topic.

댓글 수: 1

TS
TS 2016년 10월 10일
Thanks, didn't know you could make cell arrays like that. Much better than what I wanted to do!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

TS
2016년 10월 9일

댓글:

TS
2016년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by