matching the element of matrix with an array

9 views (last 30 days)
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 Comments
Mausmi Verma
Mausmi Verma on 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 on 29 Nov 2021
Edited: Jan on 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.

Sign in to comment.

Accepted Answer

Jan
Jan on 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] }

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!