Convert structure to a vector?

조회 수: 3 (최근 30일)
Yaser Khojah
Yaser Khojah 2018년 7월 25일
댓글: Yaser Khojah 2018년 7월 25일
Dear all, I need a help with accessing and converting a structure
if true
G = {'A', 'B'};
A = 1;
B = 2;
end
how can I convert it to a vector C = [1,2]
Thank you,
  댓글 수: 2
KSSV
KSSV 2018년 7월 25일
Where is structure above?
Yaser Khojah
Yaser Khojah 2018년 7월 25일
I have the data in this format which is
G = {'A', 'B'}
and just want to change the format. Maybe I'm wrong to define the type of this data.

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

답변 (1개)

Stephen23
Stephen23 2018년 7월 25일
>> G = {'A','B'};
>> V = [1,2]; % or [A,B]
>> [~,idx] = ismember(G,{'A','B'});
>> V(idx)
ans =
1 2
Or using char vectors (simpler):
>> G = 'AB';
>> V = [1,2];
>> [~,idx] = ismember(G,'AB');
>> V(idx)
ans =
1 2
  댓글 수: 3
Stephen23
Stephen23 2018년 7월 25일
편집: Stephen23 2018년 7월 25일
>> G = {'DOFF','On','On','DOFF','DOFF','SOFF','SOFF','On','DOFF'};
>> V = [1,2,3]; % same order as C:
>> C = {'DOFF','SOFF','On'};
>> [~,idx] = ismember(G,C);
>> V(idx)
ans =
1 3 3 1 1 2 2 3 1
Yaser Khojah
Yaser Khojah 2018년 7월 25일
Thank you so much. It worked :)

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by