フィルターのクリア

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

1 回表示 (過去 30 日間)
H R
H R 2023 年 1 月 11 日
コメント済み: H R 2023 年 1 月 11 日
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 Center および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by