matching the element of matrix with an array

4 次查看(过去 30 天)
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 个评论
Mausmi Verma
Mausmi Verma 2021-11-29
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 2021-11-29
编辑:Jan 2021-11-29
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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-11-29
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] }

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by