필터 지우기
필터 지우기

Replace numbers in a matrix depending on if statement

조회 수: 5 (최근 30일)
Askeladden2
Askeladden2 2020년 6월 10일
편집: Askeladden2 2020년 6월 10일
Dear all Community members,
I have a 5x5 matrix that contains only numerical values. I want to remove values below a given threshold and add the sum of these values to the next horisontal element that is above the threshold.
My input matrix is as follows:
A=[1 6 7 8 13; 1 3 11 8 15; 1 2 4 13 19; 1 3 4 8 11; 1 3 4 5 11];
Threshold value=5
I.e. I want to replace all values <=5 with zeros and add the sum of these to the next horisontal element that is above the threshold. So the output matrix shall be:
B=[0 7 7 8 13; 0 0 15 8 15; 0 0 0 20 19; 0 0 0 16 11; 0 0 0 0 24];
Can anyone assist me?
Thanks in advance.

채택된 답변

KSSV
KSSV 2020년 6월 10일
편집: KSSV 2020년 6월 10일
  댓글 수: 3
KSSV
KSSV 2020년 6월 10일
A=[1 6 7 8 13; 1 3 11 8 15; 1 2 4 13 19; 1 3 4 8 11; 1 3 4 5 11];
B=[0 7 7 8 13; 0 0 15 8 15; 0 0 0 20 19; 0 0 0 16 11; 0 0 0 0 24];
val = 5 ;
C = A ;
for i = 1:size(A,1)
idx = A(i,:)<=val ;
id = find(idx) ;
C(i,idx) = 0 ;
thesum = sum(A(i,idx)) ;
C(i,id(end)+1) = thesum+C(i,id(end)+1) ;
end
C
Askeladden2
Askeladden2 2020년 6월 10일
편집: Askeladden2 2020년 6월 10일
Dear KSSV,
Thank you very much for all your help!
I have another question, very much related to this problem, but more challenging, which I think you can assist me on.
I have two arrays and 1 matrix;
h=[0.5;1;2;2.5;3];
t=[1 2 3 4 5];
A=[1 6 7 8 13; 1 3 11 8 15; 1 2 4 13 19; 1 3 4 8 11; 1 3 4 5 11];
I want based on a variable threshold matrix, depending on the array h and t, get the same result as above.
for i = 1:size(h)
for j = 1:size(t)
thres(i,j)=3.1*sqrt(h(i))
end
end
Resulting threshold matrix will be:
thresh=[2.2 2.2 2.2 2.2 2.2; 3.1 3.1 3.1 3.1 3.1; 4.4 4.4 4.4 4.4 4.4; 4.9 4.9 4.9 4.9 4.9; 5.4 5.4 5.4 5.4 5.4];
Then I want to apply the same procedure as before, but instead of applying a constant threshold value I want to a threshold matrix. I.e. remove values below the threshold matrix and add the sum of these values (row wise) to the next element that is above the threshold.
Result:
B=[0 7 7 8 13; 0 0 15 8 15; 0 0 0 20 19; 0 0 0 16 11; 0 0 0 0 24];
I can start a new question if this is more convenient.
Thank you in advance.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by