Convert structure to a vector?

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 日

0 投票

>> 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 件のコメント

Yaser Khojah
Yaser Khojah 2018 年 7 月 25 日
編集済み: Yaser Khojah 2018 年 7 月 25 日
Thanks for sharing this answer. It is really useful but I want to work out without showing the order of A and B. For example: G has certain value which A and B but the order different than the values in V. I will share my question as:
if
G ={'DOFF' 'On' 'On' 'DOFF' 'DOFF' 'SOFF' 'SOFF' 'On' DOFF};
DOFF =1;
On =2;
DoFF =3;
end
Now I want to convert them all to something like this
V = [1 3 3 1 1 2 2 3 3 1];
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 :)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

タグ

質問済み:

2018 年 7 月 25 日

コメント済み:

2018 年 7 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by