フィルターのクリア

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