How to realize this cycle in MatLab?

1 次查看(过去 30 天)
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?

采纳的回答

Bhaskar R
Bhaskar R 2019-11-21
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by