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

 채택된 답변

KSSV
KSSV 2016년 12월 30일

1 개 추천

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];
result= [ 2 7 1 0];
a_mat = cell2mat(a) ;
for i = 1:length(b)
result(i) = length(find(a_mat==b(i)))
end

댓글 수: 1

skysky2000
skysky2000 2016년 12월 30일
That perfect KSSV, I appreciate it .
Thankssss alot

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Stephen23
Stephen23 2016년 12월 30일

1 개 추천

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

skysky2000
skysky2000 2016년 12월 30일
Thanks Stephen, What about the second question?

댓글을 달려면 로그인하십시오.

skysky2000
skysky2000 2016년 12월 30일
편집: Stephen23 2016년 12월 30일

0 개 추천

Dear KSSV, another question. Can I take the cell that number repeat it on it with same loop. like for example:
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];
result= [ 2 7 1 0];
cell-part 79= [{[1,9,79,3] [5,73,79,3]}]
cell_part 3 = [{[1,9,79,3] [2,29,16,7,3] 3 [4,74,3] [5,73,79,3] [6,56,3] [7,3]}];
cell_part 74 = {[4,74,3]}
cell_part 10 = {0};
thankss

댓글 수: 1

Stephen23
Stephen23 2016년 12월 30일
@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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2016년 12월 30일

댓글:

2016년 12월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by