Separate rows that contain certain string character

4 ビュー (過去 30 日間)
Kosta
Kosta 2017 年 1 月 10 日
コメント済み: Stephen23 2017 年 1 月 10 日
Hi all,
I would like to separate the rows of the cellarray bellow in to two cellarrays, depending on the string character on the third column, whether it ends in 'B' or 'L'.
For example, i have the cellarray:
a={
'STING ONE' 40843 '09L';
'STRING TWO' 40842 '10B';
'STRING THREE' 40841 '11L';
'STRING FOUR' 40840 '12B';
'STRING FIVE' 40839 '03L'}
I would like to split the above automatically into:
b={
'STING ONE' 40843 '09L';
'STRING THREE' 40841 '11L';
'STRING FIVE' 40839 '03L'}
and
c={
'STRING TWO' 40842 '10B';
'STRING FOUR' 40840 '12B'}
would that be possible to do?
Thanks very much in advance for your help!
Kind regards,
Kosta.

採用された回答

Stephen23
Stephen23 2017 年 1 月 10 日
編集済み: Stephen23 2017 年 1 月 10 日
>> idx = cellfun(@(s)'B'==s(end),a(:,3));
>> c = a(idx,:)
You could even put this in a function:
>> fun = @(c)a(cellfun(@(s)c==s(end),a(:,3)),:);
>> b = fun('L');
>> c = fun('B');
  2 件のコメント
Kosta
Kosta 2017 年 1 月 10 日
wow.. elegant. Thanks!
Stephen23
Stephen23 2017 年 1 月 10 日
A more efficient solution would be to run cellfun once to generate a character array of those characters:
>> vec = cellfun(@(s)s(end),a(:,3))
vec =
L
B
L
B
L
>> b = a(vec=='L',:);
>> c = a(vec=='B',:);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by