find row with certain value and put in cell
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
NA
am 18 Feb. 2019
Bearbeitet: Andrei Bobrov
am 18 Feb. 2019
B=[ 1 2
1 5
2 3
2 4
2 5
3 4
4 5
4 7
4 9
5 6
6 11
6 12
6 13
7 8
7 9
9 10
9 14
10 11
12 13
13 14];
A= [1 0 1 0 1 0 1
1 0 1 0 1 0 1
0 1 0 0 0 0 0
0 1 1 0 1 0 1
1 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 1 1 0 1
0 0 0 0 1 0 1
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 0 1
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1];
res=cell(1,size(A,2));
for i=1:size(A,2)
[row,~]=find(A(:,i)==1);
res{i}=B(row,:);
end
result= cellfun(@(x) unique(x),res,'UniformOutput',false);
I want to get this result={[1,2,5],[2,3,4],[1,2,4,5],[4,7,9],[1,2,4,5,6,9,10,11],[6,12,13],[1,2,4,5,6,9,13,14]}
0 Kommentare
Akzeptierte Antwort
Jos (10584)
am 18 Feb. 2019
result = arrayfun(@(k) unique(B(A(:,k)==1,:)).', 1:size(A,2), 'un', 0)
0 Kommentare
Weitere Antworten (2)
adi kul
am 18 Feb. 2019
Change last line to this:
result= cellfun(@(res) unique(res),res,'UniformOutput',false)
And let us know if this is what you were looking!
0 Kommentare
Andrei Bobrov
am 18 Feb. 2019
Bearbeitet: Andrei Bobrov
am 18 Feb. 2019
z = B.*permute(A,[1,3,2]);
[~,jj] = ndgrid(1:size(A,1)*2,1:size(A,2));
out = accumarray(jj(:),z(:),[],@(x){unique(x(x~=0))});
0 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!