Assign number to certain value in cell

조회 수: 7 (최근 30일)
Nickel Blankevoort
Nickel Blankevoort 2020년 10월 23일
답변: Akira Agata 2021년 11월 25일
Two cell arrays
First cell array (4x1cell):
A = As C H N
FIrst of all I would like to assign the value 1 to the first cell As, 2 to the second cell C, 3 to the tird cell H, 4 to the fourth cell N.
Second cell array (44x1cell):
B = As As N N H H H H C C C C C etc.
now I would like to create a vector X with the assigned values to the elements to the elements in B like:
X=[1 1 4 4 3 3 3 3 2 2 2 2 2 etc.]
Kind regards Nickel
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 10월 24일
Is it required that in the first step that variables named As and N and so on be created?
Nickel Blankevoort
Nickel Blankevoort 2020년 10월 24일
If I understand your question correctly the answer is no.
the elements As,N,H and C are already in the first cell array and don't need to be created.
but the values 1,2,3 and 4 do need to be assinged to those elements.
the same goes for the second cell array, the elements are already in there.
finally a new vector needs te be created with the assigned number to the elements in the first cell array,
corresponding to the elements in the second cell array.
the elements in both cell arrays change all the time so ideally the code adapts to that

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

답변 (2개)

Ahmed raafat
Ahmed raafat 2020년 10월 23일
B{23,1}=[1 1 4 4 3 3 3 3 2 2 2 2 2 etc.]
like that???
  댓글 수: 1
Nickel Blankevoort
Nickel Blankevoort 2020년 10월 24일
Hi Ahmed,
I am not too sure where that {23,1} comes from but the vector after the equal sign is correct

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


Akira Agata
Akira Agata 2021년 11월 25일
How about using ismember function? Like:
% Sample cell arrays
A = {'As', 'C', 'H', 'N'};
B = {'As', 'As', 'N', 'N', 'H', 'H', 'H', 'H', 'C'};
% Create the array X
[~, X] = ismember(B, A);
% Show the result
disp(X)
1 1 4 4 3 3 3 3 2

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by