Borrar filtros
Borrar filtros

Matrix manipulation using strcmp

3 visualizaciones (últimos 30 días)
Zach
Zach el 30 de Mzo. de 2012
Forgive me if this is a simple question that I should be able to answer myself, but I've been struggling with the proper use of this for a few days now. I'm a relatively novice MatLab user.
I have a 2xn matrix. The left column is made up of strings, the right values associated with those strings, like this
OL 4
WR 7
OL 3
AT 2
What I've been trying to do is use strcmp to pick a particular sring and then return its associated value in a new 2xm matrix consisting solely of the string (for example 'OL' and the values associated with each.
This seems like a simple problem, however, I'm stuck.
Thanks in advance for any help you can give me.

Respuesta aceptada

Thomas
Thomas el 30 de Mzo. de 2012
a = {'OL',4;'WR',7;'OL',3;'AT',2}
c=arrayfun(@(x) strcmp(x,'OL'),{a{:,1}});
d=a(c,2)
  1 comentario
Zach
Zach el 30 de Mzo. de 2012
This works perfectly, thanks.

Iniciar sesión para comentar.

Más respuestas (1)

Honglei Chen
Honglei Chen el 30 de Mzo. de 2012
a = {'OL',4;'WR',7;'OL',3;'AT',2}
b = a(cellfun(@(x) strcmp(x,'OL'),{a{:,1}}),:)
  2 comentarios
Zach
Zach el 30 de Mzo. de 2012
This answer seems correct. However, I'm getting an error that 'g' is undefined. I tried to define it as g = [], but that doesn't work and obviously I'm not understanding what the variable, g, is.
Any expansion on this would be appreciated.
Thanks again for your help.
Honglei Chen
Honglei Chen el 30 de Mzo. de 2012
sorry, had a typo, I updated the answer

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings 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