Finding how mych values contain a specific number in a matrix
Show older comments
I built the next matrix:
A=rand(100, 100)
I need to find how much values contain '3' in the first four digits. I would like to know how to do that.
Accepted Answer
More Answers (1)
David Fletcher
on 20 Apr 2018
Edited: David Fletcher
on 20 Apr 2018
Whilst I can admire the brevity of Walter's code, I might be inclined to use a more 'conventional' alternative
A=rand(100, 100);
extract=A;
count=0;
for iter=1:4
%Prepare integer value for examination
extract=extract*10;
%remove fractional part
intVals=floor(extract);
%running total of '3's in each column
count=count+sum(intVals==3);
%subtract integer portion ready for next loop
extract=extract-intVals;
%Remove numbers where 3 has been found
extract(intVals==3)=0;
end
total=sum(count);
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!