Convert structure to a vector?

3 visualizaciones (últimos 30 días)
Yaser Khojah
Yaser Khojah el 25 de Jul. de 2018
Comentada: Yaser Khojah el 25 de Jul. de 2018
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 comentarios
KSSV
KSSV el 25 de Jul. de 2018
Where is structure above?
Yaser Khojah
Yaser Khojah el 25 de Jul. de 2018
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.

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 25 de Jul. de 2018
>> 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 comentarios
Stephen23
Stephen23 el 25 de Jul. de 2018
Editada: Stephen23 el 25 de Jul. de 2018
>> 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 el 25 de Jul. de 2018
Thank you so much. It worked :)

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by