A quick way to make sure the columns in each row are decreasing for matrix A
2 views (last 30 days)
Show older comments
Hello everyone,
I have a matrix A with 100 rows and 20 columns.
I would like to ensure that for evry and each row "i", the value in column "j" is not greater than the value in row i and column "j-1". If it is, then replace it with the corresponding value in row i, column j-1 minus a very small value 0.001.
For example, if
A= [ 4, 2.1, 2.2101, 1;
20, 10, 5, 1]
. Here, for simplicity, I only consider A to have 2 rows and 4 columns.
In this example we see that for row 1, the value in column 3 is 2.2101 which is greater than the value in row 1, column 2 that is 2.1. So, I will modify A(1, 3)=A(1,2)-0.001= 2.099. Therefore, A_new= [4 2.1 2.0.099 1; 20 10 5 1].
0 Comments
Accepted Answer
dpb
on 11 Jan 2023
That'll take iterating over the columns...and could be necessary to be recursive depending upon the input data. But, for the one case of the example;
A= [ 4, 2.1, 2.2101, 1;
20, 10, 5, 1];
for i=2:size(A,2)
ix=(A(:,i)>A(:,i-1));
if any(ix), A(ix,i)=A(ix,i-1)-0.001; end
end
disp(A)
More Answers (0)
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!