A quick way to make sure the columns in each row are decreasing for matrix A
2 次查看(过去 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].
0 个评论
采纳的回答
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!