- https://www.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop
- https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
hintializing variables with different numbers and saving them in a matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sara Al Shamsi
am 5 Okt. 2017
Kommentiert: Sara Al Shamsi
am 5 Okt. 2017
Hi,
I have a question about initializing variables that contain different numbers in a matrix.
For example,
I want to obtain matrix V by using while loop:
V = [V1;V2;V3.....;Vn].
Instead of manually writing V1, V2, V3 .. in matrix V, I want it to be done by using a loop!
Thank you.
0 Kommentare
Akzeptierte Antwort
Jan
am 5 Okt. 2017
Bearbeitet: Jan
am 5 Okt. 2017
This is asked daily for many years now. See:
The answer is always the same: Don't do this. Create an array instead and use the numbers as indices. This is faster, nicer, more flexible, easier to debug, to maintain, to expand and to read.
V = zeros(1, n); % For scalars
m = 7;
M = zeros(m, n); % For column vectors
C = cell(1, n); % For variables with different sizes
for k = 1:n
V(k) = k ^ 2;
M(:, k) = rand(m, 1);
C{k} = sprintf('%g', k);
end
Weitere Antworten (0)
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!