필터 지우기
필터 지우기

nothing assing to my variable that is in if statement

조회 수: 2 (최근 30일)
arash rad
arash rad 2022년 12월 1일
댓글: Mathieu NOE 2022년 12월 1일
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

답변 (1개)

Mathieu NOE
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
arash rad
arash rad 2022년 12월 1일
Hi thank you for your answer
I did this but yet nothing happen
Mathieu NOE
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
Gt = 40
dx = 10;
Gt_new = Gt + [+dx 0 -dx];
y1 = find(x<0)
y1 = 1×4
1 3 4 5
y2 = find(x == 0)
y2 = 1×0 empty double row vector
y3 = find(x>0)
y3 = 2
y = zeros(1,5);
for i = 1:length(x)/2
if x(i) < 0
y(:,i) = Gt_new(1)
end
end
y = 1×5
50 0 0 0 0
y
y = 1×5
50 0 0 0 0

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by