How to make elements of each column zero before nth element ??
Show older comments
Hi I am trying to do something like this, I have a matrix where I have to find the min value of each column after that I have to convert elements occuring before minimum values to zero in each column.
For example:
5x4 matrix
[5 12 8 19
4 2 9 22
6 14 7 1
8 16 4 3
10 3 5 5]
The min value in each column is 4,2,4,1
so the new matrix should be
[0 0 0 0
4 2 0 0
6 14 0 1
8 16 4 3
10 3 5 5]
Can someone please help??? i really appreciate your help.Thanks
Accepted Answer
More Answers (1)
Image Analyst
on 16 May 2020
Here's one way that's easy to understand and implement:
m = [5 12 8 19
4 2 9 22
6 14 7 1
8 16 4 3
10 3 5 5]
columnMins = min(m, [], 1) % Get min value in each column.
for col = 1 : length(columnMins)
row = find(m(:, col) == columnMins(col), 1);
m(1:row-1, col) = 0;
end
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!