Use forLoop to manipulate values of elements in a specific column

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

 Réponse acceptée

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 commentaires

Thanks for the solution, but it is slightly different than I needed.
I was actually after a way to subtract a constant value from all cells in the 6th column of of a 32x7 matrix. My example (of a 5x5 zeros) was for simplicity sake.
I figured out a solution based on your previous answers - a solution that allows me to progressively add 10 to each cell of a column, but also allows me to subtract or perform any other operation (which is why I was working with a for loop):
for i=2:length(mat)
mat(i,4) = mat(i,4) + 10;
end
I am still trying to understand the syntax of your proposed solution:
mat(:,4) = 0:10:10*(size(mat,1)-1);
How could I use that same structure to subtract a constant value from the cells of column 4?
If you want to subtract a constant value from the elements in column 4, it is almost as easy:
mat(:,4) = mat(:,4) - pi
giving:
mat =
0 0 0 -3.1416 0
0 0 0 -3.1416 0
0 0 0 -3.1416 0
0 0 0 -3.1416 0
0 0 0 -3.1416 0
MATLAB automagically expands the value (here pi) to the dimensions needed to subtract it from every element in the column, vector, or matrix, as necessary.
Parenthetically, the values in a vector or matrix are referred to as ‘elements’. A cell is a particular MATLAB data type (see the documentation on Cell Arrays for details) and using ‘cell’ to mean ‘element’ can lead to confusion here on MATLAB Answers. Please understand that’s a clarification, not a criticism!
JZ
JZ le 5 Déc 2015
Modifié(e) : JZ le 5 Déc 2015
That is indeed a much simpler solution - thank you!
And thanks for the clarification on cell/element. No offense taken.
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

lsutiger1
lsutiger1 le 5 Déc 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 commentaires

Perhaps you mean
mat(i,4) = mat(i-4,4) + 10;
?
Yep. I left a comment on his question, rather than an answer, so as I was writing an actual answer I missed it.
Life of a coder.
Thanks, but I am still getting errors with both of your suggestions:
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
mat(i,4) = mat(i-4,4) + 10;
Index exceeds matrix dimensions.
mat(i) = mat(i-1,4) + 10;
Index exceeds matrix dimensions.
Suggestions?
That's because you used i-4 rather than i-1.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by