nothing assing to my variable that is in if statement
1 次查看(过去 30 天)
显示 更早的评论
hello I wrote a code and it has 5 x and 3 y .
and with different conditions of x, a value should assign to y , but when i run my code in if statement no value assign to y and I wrote y(:,i) = Gt_new(1)
what should i do ?
thank you
my code is
clc; clear all ;close all
warning off
x1 = randi([0,5],1,10);
x = zeros(1,5);
P = 50;
for i = 1:length(x1)/2
x(:,i) = x1(:,2*i) - x1(:,(2*i)-1);
end
xmin = min(x);
xmax = max(x);
Gt = 40
dx = 10;
Gt_new = Gt + [+dx 0 -dx];
y1 = find(x<0)
y2 = find(x == 0)
y3 = find(x>0)
y = zeros(1,5);
for i = 1:length(x)/2
if x < 0
y(:,i) = Gt_new(1)
end
end
0 个评论
回答(1 个)
Mathieu NOE
2022-12-1
hello
you need to index x in the for loop
for i = 1:length(x)/2
if x(i) < 0 % here
y(:,i) = Gt_new(1)
end
end
2 个评论
Mathieu NOE
2022-12-1
hello again
your original code gives y = 0 0 0 0 0
the modified code gives another output : y = 0 50 0 0 0
(depends what randi generated in first insyance)
x1 = randi([0,5],1,10);
x = zeros(1,5);
P = 50;
for i = 1:length(x1)/2
x(:,i) = x1(:,2*i) - x1(:,(2*i)-1);
end
xmin = min(x);
xmax = max(x);
Gt = 40
dx = 10;
Gt_new = Gt + [+dx 0 -dx];
y1 = find(x<0)
y2 = find(x == 0)
y3 = find(x>0)
y = zeros(1,5);
for i = 1:length(x)/2
if x(i) < 0
y(:,i) = Gt_new(1)
end
end
y
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!