# Perform AND operations with multiple matrices

4 visualizaciones (últimos 30 días)
KostasK el 9 de Abr. de 2020
Hi all,
I am performing AND operations with multiple matrices, where I have a cell array M={1x3} containing three matrices of arbitrary dimensions, and a vector c=[1x3] containing three numbers. And with those I have to perform the below operation:
M = {rand(15), rand(15), rand(15)} ;
c = rand(1, 3) ;
i = find(M{1} == c(1) & M{2} == c(2) & M{3} == c(3)) ;
Since the second dimension of M and c are set to vary to any number (i.e. it could be 4,5,6, etc.), I was wondering wether there was a way to perform the AND (&) operation in a way which could account for the above, instead of manually ammending the code every time.
KMT.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Walter Roberson el 9 de Abr. de 2020
Editada: Walter Roberson el 9 de Abr. de 2020
M = {rand(15), rand(15), rand(15)} ;
c = rand(1, 3) ;
cc = num2cell(c);
fold(@and, cellfun(@(V,C) V == C, M, cc, 'uniform', 0))
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
KostasK el 9 de Abr. de 2020
This one works well!

Iniciar sesión para comentar.

### Más respuestas (2)

Andrei Bobrov el 9 de Abr. de 2020
i = find(all(cat(3,M{:}) == reshape(c,1,1,[]),3));
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Alexandra McClernon Ownbey el 9 de Abr. de 2020
Editada: Alexandra McClernon Ownbey el 9 de Abr. de 2020
This may not be the most efficient way, but it works. I stored everything in a logical and then compared the k-dimension in that logical. I also modified to randi with a range just so I can test it
M = {randi([-1,1],15,15), randi([-1,1],15,15), randi([-1,1],15,15)} ;
c = randi(1, 3) ;
ix = false(length(M{1}(1,:)),length(M{1}(:,1)),length(c));
for j = 1:length(c)
ix(:,:,j) = M{j} == c(j);
end
[l,m,n] = size(ix);
k = 1;
for i = 1:l
for j = 1:m
if all(ix(i,j,:))
row(k) = i;
col(k) = j;
k = k+1;
end
end
end
the locations of where c = M are located in row and col.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by