댓글 수: 4

Walter Roberson
Walter Roberson 2020년 3월 5일
What should the loop do if no whatevers have absolute value <= 1 ?
I cannot tell what it is that the absolute value is being taken for.
Is it correct that you expect your while loop to take only one trip and the whole algorithm would stop immediately because it would detect that abs(A(1)-B(1)) is <= 1 and that single entry is all you need?
Robin Li
Robin Li 2020년 3월 5일
False if no abs value <=1
Yea. I want the while loop to stop and spit out TRUE as soon as it finds out abs(A(1)-B(1)) is <= 1
Robin Li
Robin Li 2020년 3월 5일
And also, not only abs(A(1)-B(1)) is <= 1; it can also abs(A(2)-B(1)) is <= 1. Are there easy ways to do so?
Walter Roberson
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
Amit 2020년 3월 5일

1 개 추천

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

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개)

카테고리

도움말 센터File Exchange에서 Wavelet Toolbox에 대해 자세히 알아보기

태그

질문:

2020년 3월 5일

댓글:

2020년 3월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by