The order of elements in an array of cells constructed using a drive

What I'm trying to do: given a 2D matrix, get the column indices of the elements in each row that satisfy a certain condition.

For example, let's say my matrix

M = [16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1]

and my condition M>6. Then my desired result will be something like

Indices = {[1 4]'; [2 3 4]'; [1 2 4]'; [2 3]';}

After reading the answers to this similar question, I came up with this partial solution using findand accumarray:

[ix, iy] = find(M>6);
Indices = accumarray(ix,iy,[],@(iy){iy});

, - , , , . , Indices{2} = [2 4 3]' [2 3 4]', , . ix 3 2, 3, 6 9. iy 2, 3 4 . ? ? , , Indices ??

+4
2

arrayfun -

idx = arrayfun(@(x) find(M(x,:)>6),1:size(M,1),'Uni',0)

celldisp(idx) -

idx{1} =
     1     4
idx{2} =
     2     3     4
idx{3} =
     1     2     4
idx{4} =
     2     3

accumarray, iy sort, , , -

Indices = accumarray(ix,iy,[],@(iy){sort(iy)})

-

>> celldisp(Indices)
Indices{1} =
     1
     4
Indices{2} =
     2
     3
     4
Indices{3} =
     1
     2
     4
Indices{4} =
     2
     3
+2

accumarray (. , ). , , , :

[iy, ix] = find(M.'>6); %'// transpose and reverse outputs, to make ix sorted
Indices = accumarray(ix,iy,[],@(iy){iy}); %// this line is the same as yours

Indices{1} =
     1
     4

Indices{2} =
     2
     3
     4

Indices{3} =
     1
     2
     4

Indices{4} =
     2
     3
+2

All Articles