Find the rows in matrix B that contain the two numbers in each row of matrix A, in every possible order
3 次查看(过去 30 天)
显示 更早的评论
I have a Nx2-matrix A, and a Mx3-matrix B, with M>N. I would like to find the rows in B that contain the two numbers in each row of matrix A, in every possible order.
Example:
A = [1 2,
2 5]
B = [1 5 6,
1 4 2,
2 9 3,
9 5 2]
Then, for the first row in A, the second row in B should be selected, and for the second row in A, the fourth one should be selected. Thus, the result should be:
C = [1 4 2,
9 5 2]
I have already solved it with for loops, find and ismember, but my matrices are quite big and it is a quite time consuming solution. Can you please help me?
1 个评论
Geoff Hayes
2015-4-11
Paula - please provide a sample of the code that you are using to determine the matrix C so that we can offer alternative suggestions. And also clarify what you mean by your matrices are quite big. What is the smallest and what is the largest matrix that you are concerned with?
采纳的回答
Mohammad Abouali
2015-4-11
编辑:Mohammad Abouali
2015-4-11
have you considered parallel processing?
You could start MATLAB parallel workers using:
parpool % if you don't it would usually start it automatically.
It takes some time for parpool to start.
Now defining your Matrices (these are too small for parallel processing, so the cost of starting parallel workers is much more than doing the processing in parallel. So use your own big matrices here. A and B are broadcast variables. so they are communicated to all workers. the cost of that communication still might not justify it. so you really need to test it on your machine and matrices to see if parallel processing solves anything here.)
A = [1 2; ...
2 5];
B = [1 5 6; ...
1 4 2; ...
2 9 3; ...
9 5 2];
Now the parallel code that checks rows of B and A
nRa=size(A,1);
nRb=size(B,1);
results=zeros(nRa*nRb,1);
parfor idx=1:(nRa*nRb)
[rB,rA]=ind2sub([nRb,nRa],idx);
results(idx)=all(ismember(A(rA,:),B(rB,:))); %#ok<PFBNS>
end
results=reshape(results,nRb,nRa);
Note that the columns in results refer to rows in A and the rows in results refers to rows in B. So if row 2 column 1 of results is set to true or 1, then it means that row 2 of matrix B contained all the members of row 1 of matrix A.
You could check all the rows as follow: (I don't know if the rows of A and B have a one to one match or it is possible that multiple rows of B contain all the elements of, let's say row 1, of matrix A.
for rA=1:nRa
fprintf('The folowing rows of B where in row %d of A:\n',rA);
fprintf('%d ',find(results(:,rA)));
fprintf('\n');
end
The folowing rows of B where in row 1 of A:
2
The folowing rows of B where in row 2 of A:
4
Now to form your matrix C do this:
[rows,cols]=find(results);
C=B(rows,:);
C =
1 4 2
9 5 2
3 个评论
Oluropo Dairo
2015-6-9
Hi Mohammad,
I have a sample dataset of the form below and I want to sort into group of A = 11 - 20, B = 21 - 30, C = 31 - 40, etc. using the second column to do the sorting. I tried this
N = sortrows(N,2); if N(:,2)<=0; N0 = N; elseif (N(:,2)>=1) & (N(:,2)<=10); N10 = N; elseif (N(:,2)>10) & (N(:,2)<=20); N20 = N; elseif (N(:,2)>20) & (N(:,2)<=30); N30 = N; elseif (N(:,2)>30) & (N(:,2)<=40); N40 = N; if N(:,2) > 40 N50 = N50(N); end
but nothing was displayed
20.13 79.52 1007
20.15 79.65 1007
19.82 80.3 1007
18.68 83.2 1006
18.55 84.3 1006
18.2 85.5 1006
17.99 85.7 1006
17.85 86.1 1006
17.45 86.7 1006
16.16 90.3 1007
16.07 88.9 1007
15.91 89.1 1007
19.87 67.48 1008
20.45 61.71 1008
21.62 53.97 1009
22.02 47.95 1009
18.54 82.9 1006
18.66 82 1006
18.46 84.1 1006
18.36 83.6 1006
24.38 54.33 1008
25.18 50.33 1008
25.9 46.73 1008
26.46 45.79 1008
30.29 31.58 1006
30.33 29.83 1006
31.76 24.36 1004
32.37 24.63 1004
32.38 26.74 1004
32.23 24.22 1004
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!