matching the element of matrix with an array

4 visualizaciones (últimos 30 días)
Mausmi Verma
Mausmi Verma el 29 de Nov. de 2021
Comentada: Mausmi Verma el 30 de Nov. de 2021
Suppose i have an array as,
A={ [2,3;2,7] [3,2;3,4;3,8] [4,3;4,5] [5,4;5,10] [7,2;7,8;7,12] }
and a matrix as B=[17,8,14,4,18]
now i want to match the element from B with A and want answer as
C={ [ ] [3,4 ; 3,8] [4] [5,4] [7,8] }
plz help
  3 comentarios
Mausmi Verma
Mausmi Verma el 29 de Nov. de 2021
A is an cell array representing some routes, and B is a matrix representing the node may or may not exist in the routes. So if any node from B exists in the any of the routes given in A, then that route will be available and if not then that route doesnt exists and the final answer should be in the way given in C. I may not be good in framing my question in a proper way, so i apologise for that
Jan
Jan el 29 de Nov. de 2021
Editada: Jan el 29 de Nov. de 2021
A general hint: The cell array contains matrices, which consist of numbers. Explain problem in the terms Matlab can work with. "routes" and "nodes" are the meaning of the numbers, but this does not matter for Matlab, so it does not for solving the problem.
There is no need for apologies: It is a standard step in solving a problem to ask questions for clarifications. Your question about Matlab are welcome here.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 29 de Nov. de 2021
A = {[2,3;2,7], [3,2;3,4;3,8], [4,3;4,5], [5,4;5,10], [7,2;7,8;7,12]};
B = [17,8,14,4,18]
B = 1×5
17 8 14 4 18
C = cell(size(A));
for iA = 1:numel(A)
a = A{iA};
match = any(ismember(a, B), 2);
if any(match)
C{iA} = a(match, :);
end
end
celldisp(C)
C{1} = [] C{2} = 3 4 3 8 C{3} = 4 3 4 5 C{4} = 5 4 C{5} = 7 8
% C={ [ ] [3,4 ; 3,8] [4] [5,4] [7,8] }
  1 comentario
Mausmi Verma
Mausmi Verma el 30 de Nov. de 2021
Thank a lot.....your answer is of so much help for me

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays 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