Counting values above a threshold within a matrix.
39 ビュー (過去 30 日間)
古いコメントを表示
Hello,
Im looking at finding a way of counting values that are >0 from a matrix, for example:-
if true %if statement used to display matrix correctly
00100
01110
11111
01110
00100
end
Would provide the output 1,3,5,3,1. Any help would light up my day.
Cheers
Gareth
1 件のコメント
Azzi Abdelmalek
2012 年 9 月 17 日
編集済み: Azzi Abdelmalek
2012 年 9 月 17 日
is this your matrix?
0 0 2 0 0
0 3 4 5 0
1 2 3 4 5
0 6 5 4 0
0 0 3 0 0
採用された回答
その他の回答 (2 件)
Babak
2012 年 9 月 17 日
you can run the below function for each of your numbers:
function num_of_1s = no1(n)
if n==0
num_of_1s = 0;
else
length = floor(log10(n))+1;
num_of_1s = 0;
for j=length:-1:1
if floor(log10(n)) == j-1
num_of_1s = num_of_1s +1;
n = n-10^(j-1);
end
end
end
end
Then say for example:
no1(1010111)
which returns
5
nah
2012 年 9 月 17 日
A simple way is:
myMat =
0 0 2 0 0
0 3 4 5 0
1 2 3 4 5
0 6 5 4 0
0 0 3 0 0
[nrows,ncols] = size(myMat);
counts_above_0 = zeros(nrows,1);
for rx = 1:nrows
counts_above_0(rx,1) = length(find(myMat(rx,:)>0));
end
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!