필터 지우기
필터 지우기

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 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