How to do this while loop
2 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275202/image.png)
4 个评论
Walter Roberson
2020-3-5
Are you to produce a vector the same size as A? A vector the same size as B? Are you permitted to use vectorized operations at all?
采纳的回答
Amit
2020-3-5
We can do it in this way :
A = [1 2 3]; B = [2 4 5];
LenA = 1;
while LenA <= length(A)
if(A(LenA)-B)<=1
out = true;
break
else
out = false;
end
LenA =LenA+1;
end
disp(out)
Hope it helps.
1 个评论
Walter Roberson
2020-3-5
We discourage people from prividing complete solutions to homework problems.
Also, this code does not impliment the original requirement to use abs().
if(A(LenA)-B)<=1
A(LenA) would be a scalar, but B is a vector, so A(LenA)-B would be a vector, and the result of comparing to 1 would be a vector. if statements are only considered true if all elements of the array of values being tested are non-zero. The above code is therefore looking to see whether there is some element of A that is within 1 of every element of B. It is not at all clear to me that is what the homework is intended to test.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!