How do I add a known vector to an existing matrix in a loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have 14 vectors from x1 to x14. I want to creat a loop that allows me to add a known vector to the previously created matrix, for example:
I have the matrix: in= [x1; x2; x3; x4; x5]
Now I want to create a loop that adds the other vectors to the matrix in. What I wrongly did was this:
for j=6:13
ta= xj; inj=[in; xj];
How do I put de x6, x7, x8 and so on into the matrix in6, in7, in8 and so on? My problema is getting the vectors by their names to work with them... The ta is a vector that I'll need in the problem.
3 Kommentare
Stephen23
am 24 Feb. 2016
The core question is "how are these variables generated?"
If they are loaded from file data then they can be easily loaded into one variable. If they are hardcoded then change the code.
Antworten (2)
James Tursa
am 24 Feb. 2016
Bearbeitet: James Tursa
am 24 Feb. 2016
There is a way to do this using the eval function, BUT you are strongly advised against this approach. Rather, you should be using cell arrays or nD matrices for this. E.g., use x{1}, x{2}, etc instead of x1, x2, etc. That way you can get at them easily in a loop as you need. E.g., see this link:
1 Kommentar
James Tursa
am 24 Feb. 2016
Bearbeitet: James Tursa
am 24 Feb. 2016
If you have been sent these variables x1, x2, etc by someone else, then please advise them to modify their code! In the meantime, you can do this up front to put them into a manageable cell array that can be easily indexed in a loop:
n = number of variables
x = cell(n,1);
for k=1:n
x{k} = eval(['x' num2str(k)]);
end
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!