Detecting Entire Column has the same integer value

Hello, I need to determine whether the entire row has the same integer value. For example;
A=[1 1;
1 0.5;
1 0.5]; The first column is all ones. Therefore, I need ans = [1 0]
However,
A=[0.5 -1
0.5 0
0.5 1]; The first column is all 0.5. I need ans = [0 0]

4 件のコメント

Adam Danz
Adam Danz 2019 年 11 月 26 日
Your title asks for identical values within the entire column but within your question you ask about identical values in rows. The two examples you gave don't make sense either because both of them show identical values in column 1 but the expected outputs you shared differ. Also, the expected outputs you shared are 1 x 2 vectors so I'm guessing that you want to focus on columns, not rows (since there are 2 cols, and 3 rows).
Image Analyst
Image Analyst 2019 年 11 月 26 日
Adam, in the second example, the result for column 1 is zero because there are no integers in that column (0.5 is not an integer).
Yunus  Yilmaz
Yunus Yilmaz 2019 年 11 月 26 日
Image Analyst is right. Let me introduce one more example
If A = [0.5 1
0.5 1
0.5 1] the ans should be ans = [0 1]
Adam Danz
Adam Danz 2019 年 11 月 26 日
編集済み: Adam Danz 2019 年 11 月 26 日
ahhh..... right.
I updated my answer to account for integers.

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

 採用された回答

Stijn Haenen
Stijn Haenen 2019 年 11 月 26 日

0 投票

Try this:
if a(:,1)==a(1,1)&mod(a(1,1),1)==0
Ans=[1 0];
else
Ans=[0 0];
end

2 件のコメント

Yunus  Yilmaz
Yunus Yilmaz 2019 年 11 月 26 日
Thank you in advance :)
Adam Danz
Adam Danz 2019 年 11 月 26 日
編集済み: Adam Danz 2019 年 11 月 30 日
This solution only analyzes the first column which would produce the following result which indicates that col 2 does not contain duplicate integers, but it does.
a=[1 1;
1 2;
1 2];
Ans = [1,0]
See my answer for a simpler and cleaner solution that generalizes to any number of columns and considers all columns.

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

その他の回答 (2 件)

Adam Danz
Adam Danz 2019 年 11 月 26 日
編集済み: Adam Danz 2019 年 11 月 26 日

0 投票

To determine which columns contain identical values,
A=[1 1;
1 0.5;
1 0.5];
B = all(diff(A,[],1) == 0,1) % B = [1,0]
To determine which columns contain identical values that are integers,
B = all(diff(A,[],1) == 0,1) & mod(A(1,:),1)==0
Image Analyst
Image Analyst 2019 年 11 月 26 日

0 投票

Here's another way that works, though probably not the most compact
A=[1 1;
1 0.5;
1 0.5] % The first column is all ones. Therefore, I need ans = [1 0]
% Set non-integers to nan
mask = A == int32(A)
[rows, columns] =size(A);
output = zeros(1, columns); % Preallocate output.
for col = 1 : columns
% Set output to 1 if all of the values equal the first value.
if all(mask(:, col))
output(col) = all(A(:, col) == A(1, col));
end
end
output % Echo to command window.
A=[0.5 -1
0.5 0
0.5 1] % The first column is all 0.5. I need ans = [0 0]
% Set non-integers to nan
mask = A == int32(A)
[rows, columns] =size(A);
output = zeros(1, columns); % Preallocate output.
for col = 1 : columns
% Set output to 1 if all of the values equal the first value.
if all(mask(:, col))
output(col) = all(A(:, col) == A(1, col));
end
end
output % Echo to command window.

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2019 年 11 月 26 日

編集済み:

2019 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by