A quick way to make sure the columns in each row are decreasing for matrix A

1 次查看(过去 30 天)
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].

采纳的回答

dpb
dpb 2023-1-11
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)
4.0000 2.1000 2.0990 1.0000 20.0000 10.0000 5.0000 1.0000

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by