how to count the number of a specific value in a cell array and how to access the index of each value
16 ビュー (過去 30 日間)
古いコメントを表示
Dear all,
i am now working with a cell array with fixed length:
VtStatus = {1,0,0,0,0,0};
and I also created 6 checkboxes to manage this cell array, e.g.: If I want to mark the checkboxes instead of the 3rd and the 5th, then this array goes like this:
VtStatus = {1,1,0,1,0,1};
Here comes the question: I want to know how many ''1'' are in this array and also the position of these ''1'' (e.g: I want to get the index of ''active checkboxes'' as a string '1-2-4-6' ). Which functions should I use?
Many thx!
0 件のコメント
採用された回答
Ameer Hamza
2020 年 11 月 18 日
編集済み: Ameer Hamza
2020 年 11 月 18 日
If possible, use numeric array for this type of data. They will be much more efficient and easy to deal with. For cell array, you can try something like this
VtStatus = {1,1,0,1,0,1};
loccation = find([VtStatus{:}]);
num = numel(loccation);
active_checkboxes = strjoin(string(loccation), '-');
Result
>> loccation
loccation =
1 2 4 6
>> num
num =
4
>> active_checkboxes
active_checkboxes =
"1-2-4-6"
0 件のコメント
その他の回答 (2 件)
João Mendes
2020 年 11 月 18 日
To know the positions of the active checkboxes you can write:
active= find([a{:}]==1)
To know how many are active, i.e., how many '1's' exist, youc an write:
number=size(active,2)
It helps?
0 件のコメント
Superficial
2020 年 11 月 18 日
編集済み: Superficial
2020 年 11 月 18 日
Convert from a cell to a vector and it's easy.
VtStatus = {1,1,0,1,0,1};
vec=[VtStatus{:}] % Convert to a vector
inds=find(vec==1) % The index of each cell equal to one
str=num2str(inds) % You can convert inds to a string if you like
count = length(inds) % The number of cells equal to one
Gives the answers:
vec =
1 1 0 1 0 1
inds =
1 2 4 6
str =
'1 2 4 6'
count =
4
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Install Products についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!