How do i count numbers ending in 3

조회 수: 6 (최근 30일)
matthieu
matthieu 2022년 8월 26일
답변: Stephen23 2022년 8월 26일
I have a bunch of numbers x=[234 352 298 213 365 321 293 213], Iwant to know how to count number ending in 3. The output should be 3

답변 (3개)

Torsten
Torsten 2022년 8월 26일
x = [234 352 298 213 365 321 293 213];
n = numel(x);
end_digit = zeros(n,1);
for i = 1:n
y = num2str(x(i));
end_digit(i) = str2num(y(end));
end
s = sum(end_digit==3)
s = 3

Abderrahim. B
Abderrahim. B 2022년 8월 26일
This:
x =[234 352 298 213 365 321 293 213] ;
xs = strsplit(num2str(x), ' ') ;
total = nnz(endsWith(xs, '3'))
total = 3

Stephen23
Stephen23 2022년 8월 26일
Simple, efficient, basic mathematics:
x = [234,352,298,213,365,321,293,213];
n = nnz(mod(x,10)==3)
n = 3

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by