changing values in a certain column of a matrix

I have a large matrix and in the fifth column I want to replace every value of 990 with 30.375, how could I do this easily? I found many similar questions but not exactly the same one and I tried to adapt the solutions to my situation but it hasn't worked yet..

3 Comments

Change the value of the 3rd column to [6 9 2 5]
YAHAYA
YAHAYA on 1 Oct 2025
Moved: DGM on 2 Oct 2025
how can i chage the value first value of the matrix to 0.5, give A(2
2
3)
See also:
For example, assigning an entire row or column using a vector of equal length:
A = magic(4) % we have a 4x4 matrix
A = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(3,:) = [2 4 6 8] % assign values to a row
A = 4×4
16 2 3 13 5 11 10 8 2 4 6 8 4 14 15 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(:,3) = [2 4 6 8] % assign values to a column
A = 4×4
16 2 2 13 5 11 4 8 2 4 6 8 4 14 8 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
For another example, use linear indexing to assign a value to the nth element:
A(6) = 100 % ordering is column-major
A = 4×4
16 2 2 13 5 100 4 8 2 4 6 8 4 14 8 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

 Accepted Answer

Cedric
Cedric on 4 May 2014
Edited: Cedric on 4 May 2014
First, you build a logical index of all values equal to 990 in column 5:
lId = A(:,5) == 990 ;
Then you use it to index relevant elements of A (in column 5), that you set to 30.375:
A(lId,5) = 30.375 ;
Let me know if you need more information.

5 Comments

Yes! That works! Thank you so much. I ended up with:
A( A (:,5) == 990,1) = 30.375;
any idea why that didn´t work? (I am already very glad I have it working now, but I'm just asking in an attempt to learn even more from my mistakes)
Almost correct. The outcome of the expression
A(:,5) == 990
is what I stored in the intermediary variable lId. It is a column vector of logicals which are true where A(:,5) is equal to 990 and false otherwise. You can use the expression directly for indexing (logical indexing) rows of A (along the first dimension), but you still have to index the correct column, i.e. column 5:
A( A(:,5)==990, 5 ) = 30.375 ;
Welcome back Cedric. It seems like you took a sabbatical for a few months.
Thank you Image Analyst! Yes, I could have warned, but I always thought that I would be able to come back "within a few days", and I lost track of time!
Is the same logic applicable on a table with single column.
My problem is 'I have to replace the values of 2,3,4 in a single column of a table to 1' I tried with your logic but getting an error. Kindly suggest the perfect logic by anyone.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 4 May 2014

Moved:

DGM
on 2 Oct 2025

Community Treasure Hunt

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

Start Hunting!