Question on creating dynamic matrix variables
Show older comments
Hi all, I would like to know how to write dynamic matrix variables. For example, I have the following:
clear all;
n=11
for i=1:n-1
A1(i,i+1) = 1;
A1(i+1,i) = 1;
end
A1
for i=1:n-2
A2(i,i+2) = 1;
A2(i+2,i) = 1;
end
A2
for i=1:n-3
A3(i,i+3) = 1;
A3(i+3,i) = 1;
end
A3
And I would like to make something like this:
for k=1:1:3
for i=1:n-1
A[k](i,i+1) = 1;
A[k](i+1,i) = 1;
end
end
Or alternatively, is there a neat way to do so?
1 Comment
Accepted Answer
More Answers (1)
Taylor
on 12 Apr 2024
0 votes
May be worth looking into structures instead of matrices. https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!