How to intersect a matrix with a set and keep the rows of the matrix?

I'm trying to intersect an N by M matrix, 'A', with a set (a 1 by X matrix), 'B', but am trying to do so by rows. (e.g. A(1:M) intersect B ).
So I would end up with an N by '__' matrix where the rows are the intersection of the respective row and set 'B', rather than a large set.
Is there a way to do this without using a for loop?

6 comentarios

Can you provide a short example for what it means to intersect an N x M matrix with a 1 x X vector and end up with a N x M matrix?
Sorry I didn't mean ending up with an NxM matrix, I just want the same number of N rows
% 2x5 matrix
A = [
2 12 6 4 8
1 3 5 7 9
]
B = [ 1 2 3 4 5 6 ]
C = intersect(A, B);
I want 'C' to be
C = [
2 6 4
1 3 5
]
Using the intersect function provided by MATLAB 'C' is
C = [
1
2
3
4
5
6
]
Your example conveniently has the same number of elements in each row of the result. What if this is not the case?
That would be a problem, wouldn't it... I keep forgetting this is not C. Would it be possible to pad rows with NaN's? (I'm assuming MATLAB functions like min and max ignore NaN's.)
There are various ways to do it (padding, cell arrays, etc) ... we just need to know what you want for an output.
Rion Wilson-Yue
Rion Wilson-Yue el 25 de Ag. de 2015
Editada: Rion Wilson-Yue el 25 de Ag. de 2015
In the very end I'm looking for the min and max of the intersection of a row and the 1xX array. I want to keep the rows so I can get the min and max for each of them.
So long as I can get that and keep the script fast, format doesn't really matter. And if you can suggest a better way of doing this I'm open to that as well.

Iniciar sesión para comentar.

 Respuesta aceptada

If you only want to get the min and max of each row, and really want to avoid for loops, here's a solution. This gives the min and max for each row of A, ignoring values not in B, which is I think what you are looking for.
A = [...
2 12 6 4 8
1 3 5 7 9
];
B = [ 1 2 3 4 5 6 ];
tf = ismember(A, B);
Amax = A;
Amin = A;
Amax(~tf) = -Inf;
Amin(~tf) = Inf;
maxval = max(Amax, [], 2);
minval = min(Amin, [], 2);
results
>> maxval
maxval =
6
5
>> minval
minval =
2
1

4 comentarios

Beautiful! Exactly what I needed, but couldn't seem to convey!
Thanks for your help everyone!
Note that for really large matrices, a for loop over the rows may be more efficient than making two extra copies of the A matrix.
Interesting... If I don't care about preserving the data could I use NaN instead of +/- Inf and then just replace the elements in 'A'?
Kelly Kearney
Kelly Kearney el 26 de Ag. de 2015
Editada: Kelly Kearney el 26 de Ag. de 2015
Yes, that would work. I always forget that min/max ignore NaNs by default.

Iniciar sesión para comentar.

Más respuestas (1)

How about a not-too-fancy but simple, intuitive, and easy to understand "for" loop?
A = randi(20, 2, 15) % Sample data
B = [ 1 2 3 4 5 6 ] % What we're looking for
C = NaN(size(A)); % Preallocate
% Check each row one at a time.
for row = 1 : size(A, 1)
% Find intersection.
B_in_A = intersect(A(row, :), B)
% Stuff them into C
C(row, 1:length(B_in_A)) = B_in_A;
end
% Display C
C

4 comentarios

Sorry, I'm trying to keep the script as fast as possible; so I'm trying to avoid 'for' loops.
You're funny. Do you know how fast or slow a for loop even is? I just did one hundred million (yes, 100,000,000) for loop iterations in 0.2 seconds. Just how fast do you need? How many iterations do you plan on doing? Trillions?
The for loop itself is rarely the reason why it's sometimes slightly slower - it's the way the memory is accessed.
Yes yes, I know how fast a for loop is. And yes odds are it would, in all probablity, be fine to use a for loop, and even then this smidgen of code would still not be the system's bottle neck. But I'm trying to move from C thinking to MATLAB thinking (using matrix math/functions), and would also like to see how fast I can make this run.
While the difference in time may be insignificant I'd still like to know if this can be done without a for loop. This is why I asked my question (see last line in original question).
Not that I can think of off the top of my head. The problem is that you want your answers row-by-row, and each row has a different number of numbers, so all I can think of is a row-by-row solution.

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Preguntada:

el 24 de Ag. de 2015

Editada:

el 26 de Ag. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by