matching the element of matrix with an array

조회 수: 4 (최근 30일)
Mausmi Verma
Mausmi Verma 2021년 11월 29일
댓글: Mausmi Verma 2021년 11월 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] }
  댓글 수: 1
Mausmi Verma
Mausmi Verma 2021년 11월 30일
Thank a lot.....your answer is of so much help for me

댓글을 달려면 로그인하십시오.

추가 답변 (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