Compare two row and select appropriate data

56 ビュー (過去 30 日間)
TW
TW 2025 年 9 月 10 日
編集済み: Matt J 2025 年 9 月 10 日
I have two columns. Let's call them column a and column b.
I want to do a check where:
if row 1 of column a > row 1 of column b, use row 1 of column a. Else, use row 1 of column b.
I have tried
if Column a > Column b
Column c = column b
else
Column c = column a
end
However, when I check the data, I find out some of the function isn't working and it just pulls all the data from column a into column c.
Basically column b is the "cap." And no number in column c should be greater than that. If any numbers in column a is greater than column b, column b should be used.

採用された回答

TW
TW 2025 年 9 月 10 日
The function I ultimately used was:
c=min(a,b)
Thanks @Matt J for pointing me in the right direction, though.
  2 件のコメント
Matt J
Matt J 2025 年 9 月 10 日
編集済み: Matt J 2025 年 9 月 10 日
But that does not match the requirements in your question. "if row 1 of column a > row 1 of column b, use row 1 of column a"
TW
TW 2025 年 9 月 10 日
Sorry my bad. Flipped the two. I wanted b to be the "cap."

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

その他の回答 (3 件)

Matt J
Matt J 2025 年 9 月 10 日
  3 件のコメント
Matt J
Matt J 2025 年 9 月 10 日
Of course. Did you try it?
TW
TW 2025 年 9 月 10 日
Yuppa. But I think I wanted the "min" variable, not max.

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


Matt J
Matt J 2025 年 9 月 10 日
編集済み: Matt J 2025 年 9 月 10 日
Basically column b is the "cap."
If so, one could also do,
a=[1;2;3;4]; b=[1;1;3;3];
c=clip(a,-inf,b)
c = 4×1
1 1 3 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 件のコメント
TW
TW 2025 年 9 月 10 日
What's the difference between clip and min?
Matt J
Matt J 2025 年 9 月 10 日
編集済み: Matt J 2025 年 9 月 10 日
In this case, there is no difference, but if you later decided you wanted both lower and upper thresholds, clip() will do that.
c = clip(a,lower,upper)

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


Star Strider
Star Strider 2025 年 9 月 10 日
I believe you want the minimum of the two columns.
Try this --
Data = array2table(randi(9, 10, 2), VariableNames=["A","B"])
Data = 10×2 table
A B _ _ 2 4 4 2 3 4 4 2 4 9 6 1 6 2 7 6 2 4 7 5
[C,idx] = min([Data.A, Data.B],[],2);
Data.C = C
Data = 10×3 table
A B C _ _ _ 2 4 2 4 2 2 3 4 3 4 2 2 4 9 4 6 1 1 6 2 2 7 6 6 2 4 2 7 5 5
OriginalColumn = idx
OriginalColumn = 10×1
1 2 1 2 1 2 2 2 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The 'idx' output is the column chosen to be Column 'C'.
.

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by