computing the cumulative matrix with some conditions

4 ビュー (過去 30 日間)
Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 3 月 6 日
回答済み: Matt J 2021 年 3 月 7 日
Is there a very concise way to code up computing the cumulative matrix of the following matrix on each column given the following condition that if the element of the column is 1 it adds 0. If it would be 2 it adds 2, if it is 3 it adds to the previous value 4 and if the element of the matrix is 4 it add to the previous value -5
AR =
1 1 1 1 1 1
1 1 1 1 1 1
1 1 2 1 2 1
1 1 2 1 2 2
1 1 1 1 1 1
1 1 1 1 1 1
2 2 2 2 2 2
2 2 2 2 2 2
1 1 1 1 1 1
1 1 1 1 1 1
1 2 2 2 2 2
2 2 2 2 2 2
1 1 1 1 1 1
1 1 1 1 1 1
2 2 2 2 2 2
2 2 1 2 2 2
1 1 1 1 1 1
1 1 1 2 1 1
2 2 1 2 2 2
2 2 1 2 2 2
1 1 1 1 1 1
1 1 1 1 1 1
2 1 2 2 2 1

回答 (2 件)

Image Analyst
Image Analyst 2021 年 3 月 6 日
Why can't you just make up a look up table like [0, 2, 4, -5] and so on. Then just do a simple for loop for the various conditions such as
  1. adding the number from the look up table to the current value, and replacing the current value with it, or
  2. adding the number from the look up table to the "previous" element's value
for case #2, I'm not sure whether the result replaces the current element or the previous element.
  2 件のコメント
Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 3 月 6 日
I did something like this right now. I was wondering whether there is a better way or not?
indices = find(AR==1);
AR(indices) = 0;
indices = find(AR==4);
AR(indices) =-5;
indices = find(AR==3);
AR(indices) =4;
ac_re = cumsum(AR,1);
Image Analyst
Image Analyst 2021 年 3 月 7 日
That's doing a pure assignment to the number you specify. I thought you wanted to add some numbers together.
Explain exactly, with a sample array, what "it adds to the previous value" means to you.
Also explain what "it adds 2" means to you.

サインインしてコメントする。


Matt J
Matt J 2021 年 3 月 7 日
Is this what you want?
T=[0, 2, 4, -5] ;
result=cumsum(T(AR))

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by