How to count repeated rows of characters using a loop?
Show older comments
Say I have an array (I have already sorted alphabetically): results=[ABC;ABC;DEF;GHI;GHI;....]. I must use either a 'for' or 'while' loop (and I believe an 'if' statement will also make this easier) to find out how many times any repeated rows appear. i.e my above results would end up as [2;1;2;...]
thank you
3 Comments
dpb
on 23 Mar 2014
For homework help, show your work to date and where you're specifically stuck...
Jan
on 23 Mar 2014
"results=[ABC;ABC;DEF;GHI;GHI;...." is no valid Matlab syntax. Is this a cell string or a CHAR matrix? Some quote characters are missing in both cases.
Jason
on 23 Mar 2014
Answers (1)
Image Analyst
on 23 Mar 2014
0 votes
Hint: have you looked into sortrows() and unique()? If I were to do it for you, that's what I'd be looking up. Or maybe use hist(). Have you tried anything yet? Is this homework?
2 Comments
Jason
on 23 Mar 2014
Image Analyst
on 23 Mar 2014
Jason, you should have tagged it as homework. Please do so now. OK, so you have a loop over rows - that should be obvious, right. Now what? How about having another loop inside over rows from 1 to the current row where you check the current row against all 3 column elements of all prior rows ? That seems to make sense. Now if a match is found, you'll need to increment something, and if no match is found, it's the first one found so far so you need to set the count array to 1 for that pattern. Just think it through and you'll get it. Come back with more code if you have trouble. We can't just hand over to you a turnkey solution.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!