Counting number in a matrix using loop

Hello, I haven't used MatLab for awhile. I am trying to make a loop function to count how many numbers are between 10 to 50 for a set of number (without using the FIND() or SUM() function. Here is my code so far:
x=[20 10 15 70 90 111 18 10 76 31 15 67 89 57 47];
count=0;
n=length(x);
for k=1:n
while k<n+1
if x(n)>10 && x(n)<50
count=count+1;
else
fprintf('No number is between 15 and 40')
end
end
end
count
I am not receiving the right 'count' number. I am also trying sum that number together afterward. Can someone help me with the code, please!

1 Kommentar

Stephen23
Stephen23 am 27 Mär. 2018
Bearbeitet: Stephen23 am 27 Mär. 2018
@Francisco: you should probably remove your email address from your user name, unless you want spambots that trawl the web to find it and send you endless quantities of spam. This is a public forum, so everyone can see your username (and email address).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 27 Mär. 2018

1 Stimme

Get rid of the while statement and index with k, not n.
x=[20 10 15 70 90 111 18 10 76 31 15 67 89 57 47];
count=0;
n=length(x);
for k=1:n
if x(k)>10 && x(k)<50
fprintf('%d is between 15 and 40.\n', x(k))
count=count+1;
else
fprintf('%d is not between 15 and 40.\n', x(k))
end
end
fprintf('I found %d elements between 15 and 40.\n', count)

4 Kommentare

sky keeper
sky keeper am 24 Apr. 2021
Sir please tell me if I count 0,1,0,1 push botton switching how to count
Image Analyst
Image Analyst am 24 Apr. 2021
@sky keeper, not sure what you mean. Explain more in a new question after you read the link below:
sky keeper
sky keeper am 24 Apr. 2021
I have a push button connect to matlab through arduino. The push swtich give value if push 0,if pull 1 . Now I want to write a function for it.how many time it is push.i want to count number.
Image Analyst
Image Analyst am 24 Apr. 2021
See attached demo. Adapt as needed.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 27 Mär. 2018

Kommentiert:

am 24 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by