Obtaining how many times a number is repeated (Matlab)

2 ビュー (過去 30 日間)
Afluo Raoual
Afluo Raoual 2021 年 3 月 23 日
コメント済み: Afluo Raoual 2021 年 3 月 23 日
Dear members;
I have vector S=[0 1 1 0 1] in which the position L of ones is
L=find(S==1); So L=[2,3,5]
I have also Bi (i=1:5) in each Bi I have the position of ones like this :
B1=[1,2,5,6,7,8] B2=[1,3,4,6,8,10] B3=[2,4,5,8,9,10] B4=[1,3,5,7,9,10] B5=[2,3,4,6,7,9]
Because L=[2,3,5] I have to go to B2, B3 and B5 and obtaining how many times each number from 1 to 10 is repeated
For example from B2, B3 and B5 (number 1 is repeated just once in B2) and (number 2 is repeated twice in B3 and B5), (number 3 is repeated twice in B2 and B5), (number 4 is repeated thrice in B2, B3 and B5) .... etc until number 10.
So please help me how can I program this in Matlab
Thank you
  1 件のコメント
KSSV
KSSV 2021 年 3 月 23 日
Read about unique and hist.

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

回答 (1 件)

Jan
Jan 2021 年 3 月 23 日
編集済み: Jan 2021 年 3 月 23 日
Do not use indices hidden in the name of the variables, but indices of arrays:
B{1} = [1,2,5,6,7,8];
B{2} = [1,3,4,6,8,10];
B{3} = [2,4,5,8,9,10];
B{4} = [1,3,5,7,9,10];
B{5} = [2,3,4,6,7,9];
S = [0 1 1 0 1];
BS = cat(2, BS{S == 1});
D = histcounts(BS, 'BinMethod', 'integers')
  4 件のコメント
Jan
Jan 2021 年 3 月 23 日
編集済み: Jan 2021 年 3 月 23 日
@Afluo Raoual: Now your input look different.
H = [1 1 0 0 1 1 1 1 0 0;
1 0 1 1 0 1 0 1 0 1;
0 1 0 1 1 0 0 1 1 1;
1 0 1 0 1 0 1 0 1 1;
0 1 1 1 0 1 1 0 1 0];
S = [0 1 1 0 1];
B = sum(H(S == 1, :), 1);
Why do you want to waste time with a loop? Do you really need the indices obtained by FIND?
Afluo Raoual
Afluo Raoual 2021 年 3 月 23 日
Thank you so much. It works now with this simple idea. I thought it can just solved with for loop.
I appreciate your help as always.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by