compare between vector and cell
Show older comments
Dear all, I have one vector (b) and one cell (a) as shown below: How I know how many times each number in vector b repeat it in a.
a=[{[1,9,79,3] [2,29,16,7,3] 3 [4,74,3] [5,73,79,3] [6,56,3] [7,3]}]
b=[ 79 3 74 10];
the expect result should be;
result= [ 2 7 1 0];
Thanks alot
1 Comment
Stephen23
on 31 Dec 2016
Question is continued here:
Accepted Answer
More Answers (2)
Stephen23
on 30 Dec 2016
A much simpler solution:
>> a = {[1,9,79,3],[2,29,16,7,3],3,[4,74,3],[5,73,79,3],[6,56,3],[7,3]};
>> b = [79,3,74,10];
>> sum(bsxfun(@eq,[a{:}],b(:)),2)
ans =
2
7
1
0
1 Comment
skysky2000
on 30 Dec 2016
skysky2000
on 30 Dec 2016
Edited: Stephen23
on 30 Dec 2016
1 Comment
Stephen23
on 30 Dec 2016
@skysky2000: that is not a good idea: naming variables dynamically will make your code slow, buggy, and hard to follow. Read this to know why:
A much better solution is to learn to use indexing, which is fast and efficient.
Categories
Find more on Matrix Indexing 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!