1

Is there an easy way that you can check if all elements in a row or column or any of the diagonals are the same thing?

a={'o','','o';
   'x','o','o';
   'x','o','o'}

How can I check if all elements in a row or column or any of the diagonals are the same thing?

2
  • 1
    What is the size (dimensions) of your cell array? give an example with desired results! Commented May 5, 2015 at 5:58
  • @SanthanSalai: I have added an example now. Commented May 5, 2015 at 6:44

2 Answers 2

2

you can employ isequal and use the fact that it can accept multiple arguments and indexing cell arrays can create comma-separated lists:

for second row:

>> isequal(a{2,:})
ans =
     0

for third column:

>> isequal(a{:,3})
ans =
     1

for diagonal:

>> isequal(a{logical(eye(size(a)))})
ans =
     1

for anti-diagonal:

>> isequal(a{flipud(logical(eye(size(a))))})
ans =
     0
Sign up to request clarification or add additional context in comments.

Comments

1

One approach with unique, few diff's and combinations of any, all -

%// Tag each cell element based on their uniqueness among other cells
[~,~,idx] = unique(a)
ar = reshape(idx,size(a))

%// Perform checks along columns, rows, diagonals and anti-diagonals
col_check = any(all(diff(ar,[],1)==0,1))
row_check = any(all(diff(ar,[],2)==0,2))
diag_check = all(diff(ar(eye(3)==1))==0)
antidiag_check = all(diff(ar(fliplr(eye(3))==1))==0)

%// Finally check if any of the checks are true for the final output
out = col_check | row_check | diag_check | antidiag_check

Sample run -

a = 
    'x'    ''     'o'    'o'
    ''     'o'    'o'    'o'
    'o'    'o'    'o'    'x'
    'o'    'o'    'o'    'o'
col_check =
     1
row_check =
     1
diag_check =
     0
antidiag_check =
     0
out =
     1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.