0

I have the data as shown in below:

    for a=1:2
    for b=1:2
        for c=1:2
            for d=1:2
                 m{a,b}{c,d}=zeros(3,3);
            end
        end
    end
end

m{1,1}{1,1}=[6 1 4;3 7 2;1 5 6]; %pass
m{1,1}{1,2}=[3 5 4;9 2 7;5 5 6];
m{1,1}{2,1}=[5 2 3;5 9 5;2 2 3];
m{1,1}{2,2}=[2 1 0;3 5 6;8 8 8];

m{1,2}{1,1}=[2 1 3;5 6 7;3 5 5];
m{1,2}{1,2}=[6 2 4;7 7 9;3 5 8];%pass
m{1,2}{2,1}=[1 2 2;4 5 5;2 6 7];
m{1,2}{2,2}=[3 3 1;2 4 5;6 7 5];

m{2,1}{1,1}=[2 5 0;3 7 6;8 6 8];
m{2,1}{1,2}=[3 6 4;3 2 7;5 8 6];
m{2,1}{2,1}=[3 9 1;2 1 5;6 2 8];
m{2,1}{2,2}=[9 1 4;9 7 7;5 2 8];%pass

m{2,2}{1,1}=[0 5 0;3 2 6;8 6 9];
m{2,2}{1,2}=[1 2 7;5 2 5;4 2 6];
m{2,2}{2,1}=[2 1 3;2 4 6;6 9 5];
m{2,2}{2,2}=[3 7 1;1 4 5;6 9 3];

I would like to take the diagonal of each set of data and check whether all these numbers are larger than five .

For instance, the diagonal for m{1,1}{1,1} is 6,7,and 6 respectively which larger than five,thus it fullfill the condition.

Moreover,I need to know the position of dataset which pass the condition.In this example, the result of dataset m{1,1}{1,1} ,m{1,2}{1,2} and m{2,1}{2,2} are pass.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tony YEe
  • 235
  • 1
  • 3
  • 13

2 Answers2

2
t=cellfun(@(mii) cellfun(@(mjj) all(diag(mjj)>5),mii),m,'uni',false);

t = 
    [2x2 logical]    [2x2 logical]
    [2x2 logical]    [2x2 logical]

t{1,1} =
     1     0
     0     0

so m{1,1}{1,1} is true, m{1,1}{1,2} is false, etc

Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
  • 1
    I cant believe for all those years I could have written 'uni' instead of 'uniformoutput'. thanks! – Mercury Mar 16 '13 at 14:20
1
   a=cellfun(@(x) cellfun(@(y) all(diag(y)>5),x),m,'uniformoutput',false)

you can find the answer inside a :)

Mercury
  • 1,886
  • 5
  • 25
  • 44
  • what is x and y represent for??Thanks fro explanation~ – Tony YEe Mar 16 '13 at 12:31
  • Tony YEe: you have cells in cell array, co the outer cell fun runs on m cells (this is x). in each cell you need to process the inner cells - this is the inner cellfun, which runs on y. – Mercury Mar 16 '13 at 14:17