array counting in not happening

4 ビュー (過去 30 日間)
Rajib Panja
Rajib Panja 2021 年 6 月 29 日
コメント済み: Rajib Panja 2021 年 6 月 29 日
numbers_to_extract_normal=[278,199,241,235,35,77,82,164,239,170];
numbers_to_extract_diagnosed = [139,137,136,135,133,140,141,116,157,188];
normal = 0;
diagnosed =0 ;
digits_normal = numel(num2str(numbers_to_extract_normal(i)));
Error: Array indices must be positive integers or logical values.
please help me

回答 (2 件)

Jan
Jan 2021 年 6 月 29 日
The variable i is not defined. If you omit it, num2str() replies a cell array and numel does not work directly. With cellfun():
digits_normal = cellfun('length', num2str(numbers_to_extract_normal));
Converting the numbers to char vectors only to count the number of digits is not efficient. Smarter:
digits_normal = floor(log10(numbers_to_extract_normal)) + 1;
  1 件のコメント
Rajib Panja
Rajib Panja 2021 年 6 月 29 日
thank you sir

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


DGM
DGM 2021 年 6 月 29 日
編集済み: DGM 2021 年 6 月 29 日
Your index i isn't valid because it's a complex number.
i = sqrt(-1);
This is a good example of why you shouldn't be using i or j as iterators or indices.
This should also be a lesson in actually defining your variables before you use them.

カテゴリ

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