3 views (last 30 days)

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]

Stijn Haenen
on 26 Nov 2019

Try this:

if a(:,1)==a(1,1)&mod(a(1,1),1)==0

Ans=[1 0];

else

Ans=[0 0];

end

Adam Danz
on 26 Nov 2019

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.

Sign in to comment.

Adam Danz
on 26 Nov 2019

Edited: Adam Danz
on 26 Nov 2019

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

Sign in to comment.

Image Analyst
on 26 Nov 2019

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.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771733

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771733

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771735

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771735

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771736

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771736

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771741

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/493353-detecting-entire-column-has-the-same-integer-value#comment_771741

Sign in to comment.