필터 지우기
필터 지우기

Testing for identical numbers in matrix columns

조회 수: 4 (최근 30일)
Konstantin
Konstantin 2011년 3월 3일
댓글: MICHAEL MONT-ETON 2020년 7월 22일
Hello!
I'm not a stupid, I just want to learn to use MATLAB effectively.
I have a matrix. In my work it is 64x64, for example let's see this one
A =
1 2 3 4
2 3 3 4
6 7 2 4
I want to now, in which columns all numbers are equal. I know how to do it for one column
A = [1,2,3,4; 2,3,3,4; 6,7,2,4];
REZ(1) = all((A(1,1)==A(:,1)));
Of course I can do this operation in cycle, but i think there is method to do it in one line.
If I run
>> X=(1:4);
>> REZ_2(X)=((A(1,X)==(A(:,X))));
It doesn't work, with error
??? Error using ==> eq
Matrix dimensions must agree.
How to solve this problem elegant?
  댓글 수: 2
Andrew Newell
Andrew Newell 2011년 3월 3일
Konstantin, it's not a stupid question, and it might even pick up a vote or two if you change the title to a more descriptive one (for example, "Which matrix columns have all numbers equal?").
Andrew Newell
Andrew Newell 2011년 3월 3일
There you go - one vote already!

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Tearle
Matt Tearle 2011년 3월 3일
all(~diff(A))
Like Andrew's idea, but a bit neater(?). The not (~) converts the differences to logicals (0 difference -> true). Then all sees if all the differences in a column are 0.
Just for fun, here's another possibility:
all(bsxfun(@eq,A,A(1,:)))
(This actually has one advantage, in that it would work even if A has only one row!)
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2011년 3월 3일
My votes goes to the BSXFUN solution.
Andrew Newell
Andrew Newell 2011년 3월 3일
I prefer the ALL version because it's not very interesting to say that each element is equal to itself.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jos (10584)
Jos (10584) 2011년 3월 3일
Nothing shorter than:
~std(A)
  댓글 수: 4
Oisín Moran
Oisín Moran 2018년 1월 17일
편집: Oisín Moran 2018년 1월 17일
I know this was 7 years ago but for anyone coming across this now it is not guaranteed to work. For example:
~std(0.9*ones(6,1))
Returns
ans =
logical
1
Whereas
~std(0.9*ones(7,1))
Returns
ans =
logical
0
Jan
Jan 2018년 1월 17일
@Oisín Moran: You are right:
std(0.9*ones(7,1))
>> 1.1992e-16
A rounding problem. This is not reliable also:
std(A) < 10*eps
The limit must reflect the size of the inputs and the summation to get mean(a), which is required for std is not stable.

댓글을 달려면 로그인하십시오.


Andrew Newell
Andrew Newell 2011년 3월 3일
Here is one way:
find(~sum(abs(diff(A))))
The idea is that diff takes the differences between adjacent terms in a column, and if all the elements are the same then sum(abs(diff(A)))=0 (the abs is needed in case positive and negative differences cancel out). Then ~sum(abs(diff(A)))=1 only for columns in which all the elements are equal.
  댓글 수: 2
Konstantin
Konstantin 2011년 3월 3일
Thank you very much!
I love this community)
MICHAEL MONT-ETON
MICHAEL MONT-ETON 2020년 7월 22일
Thanks for this solution. I plan on using it in my research into inverse solutions for the groundwater equation- a variant of the Laplace equation. It will be used to determine if a particular row or column in the input array is homogeneous. I will credit Mr.Newell in the code, and in publication.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by