Compare Row & only keep the highest value

1 回表示 (過去 30 日間)
Aninda Maji
Aninda Maji 2021 年 2 月 3 日
編集済み: Aninda Maji 2021 年 2 月 3 日
I have a matrix like this:
A:
100 0.35
200 0.37
300 0.42
400 0.40
500 0.39
600 0.46
I want the output like this:
B:
100 0.35
200 0.37
300 0.42
400 0.42
500 0.42
600 0.46
I.e. what I need is in the second column compare each row against previouos row. In case current row is higher than the previous row then keep it but if the previous row is higher then copy the prvious row in the current row.
I am aware that I can do it via a for loop. But is there any way to do it a more clean way through matrix operation?
Many thanks in advance for your help.

採用された回答

Jan
Jan 2021 年 2 月 3 日
編集済み: Jan 2021 年 2 月 3 日
A = [100, 0.35; ...
200, 0.37; ...
300, 0.42; ...
400, 0.40; ...
500, 0.39; ...
600, 0.46];
B = [A(:, 1), cummax(A(:, 2))]
Or in your case:
B = cummax(A, 1) % Along 1st dimension
  1 件のコメント
Aninda Maji
Aninda Maji 2021 年 2 月 3 日
編集済み: Aninda Maji 2021 年 2 月 3 日
Super this works as expected,
Now I have the matrix like this:
A =
[100 0.35;
200 0.38;
300 0.40;
400 0.40;
500 0.42;
600 0.42;
700 0.42;
800 0.48];
B =
[100 0.35;
200 0.38;
300 0.40;
400 0.41;
500 0.42;
600 0.44;
700 0.46;
800 0.48];
So what this B does is it check the data points to calculate where we have same values there it evenly distributes between the 2 points. so for 400 data becomes to 0.41 (0.40+(0.42 - .40/2)) and for 600, 700 data becomes 0.44 and 0.46 respectively (0.42+((.48-.42)/3)*1) & (0.42+((.48-.42)/3)*2).
Is there any simple of way of doing it?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by