"Invalid use of operator." doesn't go even after trying various solutions :(

조회 수: 2 (최근 30일)
Smit
Smit 2023년 11월 2일
댓글: Steven Lord 2023년 11월 2일
Hello,
I have written this function in matlab:
function[magnitude, phase]= cartesiantopolar(z1,z2)
for (z1~=0)&& (z2~=0)
% check for errors
if isempty(z1)||isempty(z2)
return
else
mag=sqrt(z1^2 + z2^2)
pha=atan(z2/z1)
magnitude(mag(1,:))
phase(pha(1,:))
%polar=coefficient_of_polar*exp(winkel*i)
end
end
end
I have been receivin this error for the "for (z1~=0)&& (z2~=0)" line:
Invalid use of operator.
I have already read mukltiple posts and tried multiple possibles olutions but none seem to work, does someone perhaps have any ideas on how to overcome this specific error?
I'd really appreciaste any ideas or input of any kind:)
  댓글 수: 3
Smit
Smit 2023년 11월 2일
z1 and z2 are supposed to be inputs given by the user, and the for loop would perform some operations on these inputs if ando nly if one of the two are not 0, hence the use of a for... or... not equal to 0 condition
Stephen23
Stephen23 2023년 11월 2일
"I have already read mukltiple posts and tried multiple possibles olutions..."
Did any those solutions include reading the FOR documentation?
z1 and z2 are supposed to be inputs given by the user, and the for loop would perform some operations on these inputs if ando nly if one of the two are not 0, hence the use of a for... or... not equal to 0 condition"
That does not explain why you need a loop.
From what you explain, an IF would suffice. Or if you need to handle array inputs, some logical indexing.

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

답변 (2개)

Alan Stevens
Alan Stevens 2023년 11월 2일
Why nor just use Matlab's inbuilt cart2pol function?
  댓글 수: 1
Smit
Smit 2023년 11월 2일
thhe task was to write a function that performs the same task, which is what we tried to do, for any given input z1 and z2

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


Steven Lord
Steven Lord 2023년 11월 2일
That doesn't match any of the allowed syntaxes shown on the for keyword documentation page.
If you want the code to run only if all elements of z1 and z2 are non-zero, you want to use the if keyword instead. [Note the description of how if handles non-scalar conditions. Depending on what you want to do you may need to use the any or all functions around your condition.]
If you wanted it to do something else, please explain in more detail (in words not code) what you're trying to achieve and we may be able to suggest the right way to write that code.
  댓글 수: 2
Smit
Smit 2023년 11월 2일
We want to be able to insert as many numbers in to our function as selected by the user starting at many as 2 numbers. And at the end give the numbers out of the function devided into two factors which are then given into a second function which divdes them into seperates numbers again and runs a conversion of the numbers.
Steven Lord
Steven Lord 2023년 11월 2일
So what do you want the elements of magnitude and phase whose corresponding elements in x1 and/or x2 are 0 to contain? So what if I had the following?
x1 = [0 1 0 -1 0 0 1];
x2 = [1 0 -1 0 1 0 1];

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

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by