Filter löschen
Filter löschen

Replace numbers in a matrix depending on if statement

5 Ansichten (letzte 30 Tage)
Askeladden2
Askeladden2 am 10 Jun. 2020
Bearbeitet: Askeladden2 am 10 Jun. 2020
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.

Akzeptierte Antwort

KSSV
KSSV am 10 Jun. 2020
Bearbeitet: KSSV am 10 Jun. 2020
  3 Kommentare
KSSV
KSSV am 10 Jun. 2020
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 am 10 Jun. 2020
Bearbeitet: Askeladden2 am 10 Jun. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by