How to realize this cycle in MatLab?
3 views (last 30 days)
Show older comments
Serhii Sheinych
on 21 Nov 2019
Commented: Serhii Sheinych
on 21 Nov 2019
int i;
int y1;
int y2;
int y3;
int y4;
int y5;
int y6;
int y7;
for (i=0; i<100; i++){
if(x[i]>=m && x[i]<m+s)
y1++;
else if (x[i]>=m+s && x[i]<m+2*s)
y2++;
else if (x[i]>=m+2*s && x[i]<m+3*s)
y3++;
else if (x[i]>=m+3*s && x[i]<m+4*s)
y4++;
else if (x[i]>=m+4*s && x[i]<m+5*s)
y5++;
else if (x[i]>=m+5*s && x[i]<m+6*s)
y6++;
else if (x[i]>=m+6*s && x[i]<=m+7*s)
x is a 1x100 array
m is a minimum value of an array
s is a step (constant value)
Can You help to rewrite this cycle using Matlab?
0 Comments
Accepted Answer
Bhaskar R
on 21 Nov 2019
In MATLAB no need of variable definations you can directly initialize
x = rand(1,100);% fake data 1x100 random float numbers
m = min(x); % minimum of x
s = 0.1; % assumed step value
% y values initialized to 0
y1 = 0; y2 = 0;y3 = 0;y4 = 0;y5 = 0;y6 = 0;y7 = 0;
% loop for 100 values of x
for ii =1: length(x)
if (x(ii) >= m && x(ii) < (m+s))
y1 = y1 + 1;
elseif (x(ii) >= m+s && x(ii) < (m+2*s))
y2 = y2 + 1;
elseif (x(ii) >= m+2*s && x(ii) < (m+3*s))
y3 = y3 + 1;
elseif x(ii) >= m+3*s && x(ii) < (m+4*s)
y4 = y4 + 1;
elseif x(ii) >= m+4*s && x(ii) < (m+5*s)
y5 = y5 + 1;
elseif x(ii) >= m+5*s && x(ii) < (m+6*s)
y6 = y6 + 1;
elseif x(ii) >= m+6*s && x(ii) < (m+7*s)
y7 = y7 + 1;
end
end
More Answers (0)
See Also
Categories
Find more on Statics and Dynamics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!