Azzera filtri
Azzera filtri

Matrix manipulation using strcmp

3 visualizzazioni (ultimi 30 giorni)
Zach
Zach il 30 Mar 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.

Risposta accettata

Thomas
Thomas il 30 Mar 2012
a = {'OL',4;'WR',7;'OL',3;'AT',2}
c=arrayfun(@(x) strcmp(x,'OL'),{a{:,1}});
d=a(c,2)

Più risposte (1)

Honglei Chen
Honglei Chen il 30 Mar 2012
a = {'OL',4;'WR',7;'OL',3;'AT',2}
b = a(cellfun(@(x) strcmp(x,'OL'),{a{:,1}}),:)
  2 Commenti
Zach
Zach il 30 Mar 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 il 30 Mar 2012
sorry, had a typo, I updated the answer

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by