Getting rows from dataset with particular value

2 ビュー (過去 30 日間)
Neesha
Neesha 2014 年 10 月 27 日
コメント済み: Neesha 2014 年 10 月 27 日
Hi All,
I have a dataset 'Resturants' which has a column with title 'Name'. Now all restaurants name are different but few of them have particular string in prefix like some of them start with 'genName' and followed by something e.g. 'genNameSouthIndian', 'genNameWestLocation'.
So out of my dataset i want to grab all rows which does not have 'genName' prefix. How do i do that?

採用された回答

Kelly Kearney
Kelly Kearney 2014 年 10 月 27 日
isgen = ~cellfun('isempty', regexp(restInfo.Name, '^genName'));
restInfo(~isgen,:)
ans =
val1 val2 Name
1 13 'coolEatery'
3 9 'Taj'
0 0 'TheVillage'
  1 件のコメント
Neesha
Neesha 2014 年 10 月 27 日
What does '^' do before 'genName'? I used 'genName*' but with both i m getting the same results. Hence curious

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

その他の回答 (2 件)

Adam
Adam 2014 年 10 月 27 日
I'm not too familiar with the 'dataset' structure if that is what you are using (incidentally Matlab R2014b notes this may be removed in future releases as you should use 'table' instead), but the following:
idx = ~strncmp( str, 'genName', 7 );
will give you the row indices you want if 'str' is your column in question, e.g.
str = Restaurants{:,3};
if it were the 3rd column.
Then you should just be able to use logical indexing with idx to retain only rows that do not have the 'genName' prefix in the chosen column. Again though I'm not familiar if you can do logical indexing on rows of a dataset object, but I would have thought you can.
e.g.
myRestaurants = Restaurants{ :, idx };
  3 件のコメント
Adam
Adam 2014 年 10 月 27 日
That may not be correct syntax for a dataset (if that is the structure you are using), but I'm really not familiar with them enough to give a better guess at the moment. Maybe someone else can help with that last part.
Does the code prior to it work though to give you the indices to the correct rows?
Neesha
Neesha 2014 年 10 月 27 日
Yes prior work gives right indices. I have accepted kelly's answer since that worked for me. Thanks

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


Neesha
Neesha 2014 年 10 月 27 日
so here is my code, can you help me out please:
restInfo = dataset();
restInfo.val1 = 1;
restInfo.val1(2, 1) = 1;
restInfo.val1(3, 1) = 2;
restInfo.val1(4, 1) = 3;
restInfo.val1(5, 1) = 0;
restInfo.val2 = 11;
restInfo.val2(2, 1) = 13;
restInfo.val2(3, 1) = 0;
restInfo.val2(4, 1) = 9;
restInfo.val2(5, 1) = 0;
restInfo.Name = {'genNameSouthLocation'};
restInfo.Name(2, 1) = {'coolEatery'};
restInfo.Name(3, 1) = {'genNameMexican'};
restInfo.Name(4, 1) = {'Taj'};
restInfo.Name(5, 1) = {'TheVillage'};
  1 件のコメント
Neesha
Neesha 2014 年 10 月 27 日
This creates dataset

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

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by