MATLAB Answers


Referencing specific dimensions of an array

Asked by alexander on 30 Dec 2014
Latest activity Commented on by alexander on 30 Dec 2014
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;
as well as several other variations. how can I imply to evaluate only each row?


Sign in to comment.

1 Answer

Answer by Sean de Wolski
on 30 Dec 2014
 Accepted Answer

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


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 )
if length(s)==3
c=true;else c=false;
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?

Sign in to comment.