Referencing specific dimensions of an array

조회 수: 1 (최근 30일)
alexander
alexander 2014년 12월 30일
댓글: alexander 2014년 12월 30일
Hello,
I have a rectangular matrix and I am trying to determine if values in each row are equal. I built a function that will check an array to see if all numbers are equal. I want to avoid using a for loop.
I tried to add a fourth column to an existing matrix with the result of the function.
I tried;
x(:,4)=three(x(:,:))
as well as several other variations. how can I imply to evaluate only each row?

채택된 답변

Sean de Wolski
Sean de Wolski 2014년 12월 30일
x(:,4) = all(bsxfun(@eq,x(:,1),x(:,2:3)),2)
The fourth column will not be true where the first three columns are equal.
I believe this is what you want. If it's not, exactly what do you want? A small example with inputs->operation->expected output would be ideal
  댓글 수: 2
alexander
alexander 2014년 12월 30일
thanks fo rthe answer
it took me a little while to see what you did there but I think I understand. I am not just looking for equality though. I am checking if they are all the same value. Below is my funtion
function [ c ] = three( x )
s=find(x==6);
if length(s)==3
c=true;else c=false;
end
end
alexander
alexander 2014년 12월 30일
So my way works but you's is better. I used
x(:,4) = all(bsxfun(@eq,[6],x(:,1:3)),2)
Where I can replace the 6 with whatever number I require. I still wonder though from the original question. is their some syntax that would allow what I attempted?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by