필터 지우기
필터 지우기

How to do multiple replacements in a cell array

조회 수: 1 (최근 30일)
012786534
012786534 2020년 1월 31일
댓글: 012786534 2020년 1월 31일
Hi,
I am wondering what is the most efficient way in Matlab to do multiple replacements in a cell array based on the content of a differentt cell array ?
More specifically, I am trying to transform cell1 into cell3 based on the associations defined in cell2 in the most efficient way possible (in other words cell1(:,2) becomes cell3(:,2))
% Cell array data
cell1 = {1, 2, 3, 4, 5; 'A', 'A', 'B', 'C', 'B'; 'AA', 'BB', 'CC', 'DD', 'JJ'}'
% Cell array reference
cell2 = {'C', 'B', 'A'; 'FFFF', 'GGGG', 'HHHH'}'
% Desired output
cell3 = {1, 2, 3, 4, 5; 'HHHH', 'HHHH', 'GGGG', 'FFFF', 'GGGG'; 'AA', 'BB', 'CC', 'DD', 'JJ'}'
Thank you,
  댓글 수: 2
James Tursa
James Tursa 2020년 1월 31일
Are the letters in cell1 column 2 always 'A', 'B', or 'C'?
Stephen23
Stephen23 2020년 1월 31일
Bluegin's "Answer" moved here:
No, unfortunately. There's about 50 differents codes in reality.

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

채택된 답변

Stephen23
Stephen23 2020년 1월 31일
편집: Stephen23 2020년 1월 31일
>> [idx,idy] = ismember(cell1(:,2),cell2(:,1));
>> cell3 = cell1;
>> cell3(idx,2) = cell2(idy(idx),2)
  댓글 수: 1
012786534
012786534 2020년 1월 31일
Neat, thank you. Could the same thing be accomplished with intersect ?
Something along the lines of :[C,ia,ib] = intersect(A,B, 'stable'); ?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by