matching the element of matrix with an array

4 vues (au cours des 30 derniers jours)
Mausmi Verma
Mausmi Verma le 29 Nov 2021
Commenté : Mausmi Verma le 30 Nov 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 commentaires
Mausmi Verma
Mausmi Verma le 29 Nov 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 le 29 Nov 2021
Modifié(e) : Jan le 29 Nov 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.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 29 Nov 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 commentaire
Mausmi Verma
Mausmi Verma le 30 Nov 2021
Thank a lot.....your answer is of so much help for me

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by