Use forLoop to manipulate values of elements in a specific column
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
JZ
am 5 Dez. 2015
Kommentiert: lsutiger1
am 6 Dez. 2015
I created the following 5x5 matrix with no data:
mat = zeros(5:5)
mat =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
I would like to use a for loop to generate the following result
mat =
0 0 0 0 0
0 0 0 10 0
0 0 0 20 0
0 0 0 30 0
0 0 0 40 0
I attempted using the following code, but the operation was performed on the first column - not the 4th, as I had intended.
for i = 2:length(mat(:,4))
mat(i) = mat(i-1) + 10;
end
This was the actual result
mat =
0 0 0 0 0
10 0 0 0 0
20 0 0 0 0
30 0 0 0 0
40 0 0 0 0
I imagine this is a simple syntax problem, but I am not finding the solution in any of the forums. Any help is appreciated.
Thanks
0 Kommentare
Akzeptierte Antwort
Star Strider
am 5 Dez. 2015
This works:
mat = zeros(5:5);
mat(:,4) = 0:10:10*(size(mat,1)-1);
mat =
0 0 0 0 0
0 0 0 10 0
0 0 0 20 0
0 0 0 30 0
0 0 0 40 0
4 Kommentare
Weitere Antworten (1)
lsutiger1
am 5 Dez. 2015
You have only specified the row that you want to manipulate; you need to specify both the row and column.
mat(i) = mat(i-1,4) + 10;
4 Kommentare
Siehe auch
Kategorien
Mehr zu Install Products finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!