MATLAB Answers

How can I find and remove the rows starting with different word?

21 ビュー (過去 30 日間)
Ergün AKGÜN
Ergün AKGÜN 2018 年 3 月 29 日
コメント済み: Ergün AKGÜN 2018 年 3 月 29 日
Hi All;
I have a 40000 row document. The words 1 to 485 start with the letter "a". The words 486 to 1158 start with the letter "b". It starts with the letter "c" between 1158 and 4000... There are some erroneous places among these word groups. For example, there are words starting with another letter such as "b, c, d, v ..." in groups of words that should start with the letter "a" between 1 and 485. How can I find and remove the rows of these?
Thank you for your help.

  5 件のコメント

表示 2 件の古いコメント
Ergün AKGÜN
Ergün AKGÜN 2018 年 3 月 29 日
Hi Guillaume;
It's a cell array name "Cnew".
10×1 cell array
{'a, A' }
{'ab' }
{'aba' }
{'aba altından değnek (sopa) göstermek'}
{'aba gibi' }
{'aba güreşi' }
{'aba vakti yaba, yaba vakti aba' }
{'abacı' }
{'abacı kebeci, ara yerde sen neci?' }
{'abacılık' }
Guillaume
Guillaume 2018 年 3 月 29 日
So, the problem is
  • extracting the first letter of each line
  • finding out which ones are out of order
That's fairly straightforward but for one thing: You're obviously not using an english alphabet (turk?) whose order may not match matlab's idea of order. For example
>> sort('aba güreşi değnek göstermek')
ans =
' aabdeeeeeggikkmnrrstöüğş'
is probably the wrong order.
If the alphabet for 1st letter is just US-ASCII [a-z] then it's easy.
Ergün AKGÜN
Ergün AKGÜN 2018 年 3 月 29 日
I'm so sorry about my poor english; As you see in the table in "C" letter, there is sentence starting with "a" Example: 'adamlarına ve ondan yana....'.
Here is the problem:
  1. 110:1200 rows contains words starting with "C" letter.
  2. I want to scan 110:1200 and find words which are not starting with letter "c"
  3. And delete that row
In this table i want to detect 6th and 14th row (because its not starting with "c") and delete.
{'ceket' }
{'ceketatay' }
{'celâdet' }
{'celâl' }
{'Celâlî' }
{'adamlarına ve ondan yana olanlara, sonraları da türeyen bütün eşkıyaya verilen ad.'}
{'Celâlîlik' }
{'celâllenme' }
{'celâllenmek' }
{'celâlli' }
{'celâllice' }
{'celbe' }
{'celep' }
{'yetiştirilen genç.' }
{'celeplik' }
{'celî' }
{'celî yazı celil' }
{'cellât' }
{'cellât gibi' }
{'cellâtlık' }
{'celp' }

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

採用された回答

Elias Gule
Elias Gule 2018 年 3 月 29 日
I hope this helps.
Cnew = cellfun(@char,Cnew,'uni',0); % convert the cell array contents to string
index = cellfun('isempty',(regexpi(Cnew,'^c.*'))); % find index of line not starting with C
Cnew(index) = []; % remove line not starting with C

  3 件のコメント

Ergün AKGÜN
Ergün AKGÜN 2018 年 3 月 29 日
Hi Elias; Is it possible to specify a clear range? Because, there is over 40.000 words and for example i want to clear only the range (300:1000)
Elias Gule
Elias Gule 2018 年 3 月 29 日
yep. the index variable gives you an array of 1s and 0s. so to remove rows within your specified range, you can modify the index variable like so:
Cnew = cellfun(@char,Cnew,'uni',0); % convert the cell array contents to string
index = cellfun('isempty',(regexpi(Cnew,'^c.*'))); % find index of line not starting with C
index(1:299) = 0;
index(1001:end)=0;
Cnew(index) = []; % remove line not starting with C
Ergün AKGÜN
Ergün AKGÜN 2018 年 3 月 29 日
Thank you very much, both answers are correct bur Elias answer is what exactly i need.
Thank you Elias and Guillaume

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

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 3 月 29 日
Your english is fine and I understood what you want to do.
If the first letter of every line is belong to the character set [a-z] (and it looks like you want to ignore case), then it's very easy to solve.
However, if we have to take into account accented letters such as ğ then it's a lot more complicated because matlab has no concept of internationalisation. I have no idea where ğ is located in your alphabet but it's not going to be where matlab think it is.
If we assume US-ASCII alphabet only, the intruders can be detected easily:
firstletter = lower(cellfun(@(s) s(1), Cnew)).'; %get first letter and convert to lower case
ldiff = sign(diff(firstletter));
outoforderrows = union(strfind(ldiff, [-1 1]), strfind(ldiff, [1 -1])) + 1
But with turkish alphabet, lower may not work correctly for a start. In addition, since matlab may have the wrong idea about the order of letters, it may tell you that some lines are out of order when they are not.

  0 件のコメント

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by