MATLAB Answers

# How do I use an if-else structure inside a for loop to investigate if each component of a row vector is inside or outside the boundaries?

조회 수: 2(최근 30일)
Shushlet 2021년 7월 22일
답변: Risuna Maluleke 2021년 7월 27일
Consider to have three variables named a, b, c with values 1, 10, 20 respectively.
Store these variables into a row vector called vec
Set two boundries:
• A lower limit that is low=5
• A upper limit that is up=15
Write a code to to investigate if each component of the vector vec is inside or outside the boundries.
Store the results in the vector variable out_vec with the following scheme:
• out_vec(i)=1 inside the boundry
• out_vec(i)=0 outside the boundry
Use an if-else structure inside a for loop in your code.
a = 1;
b = 10;
c = 20;
vec = [a, b, c];
for i = 5:16
if 5<=vec<=15;
print("Inside the boudries");
else
print("Outside the boundries");
end
end

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

### 답변(2개)

Matt J 2021년 7월 22일
Hint:
for i=1:numel(vec)
v=vec(i);
end
##### 댓글 수: 1표시숨기기 없음
Shushlet 2021년 7월 22일
Thank you very much for your prompt response.
However, I do not think I understand the hint and what I am supposed to do.
May you please elaborate and explain what I am supposed to do.
I highly appreciate your help.
Thank You.
a = 1;
b = 10;
c = 20;
vec = [a, b, c];
for i=1:numel(vec)
v=vec(i);
if 5<=vec<=15;
print("Inside the boudries");
else
print("Outside the boundries");
end
end

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

Risuna Maluleke 2021년 7월 27일
a = 1;
b = 10;
c = 20;
vec = [a, b, c];
for i=1:numel(vec)
v=vec(i);
if 5<=vec<=15;
print("Inside the boudries");
else
print("Outside the boundries");
end
end

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

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by