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

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 Comments

for (z1~=0)&& (z2~=0)
What exactly do you intend to do with this line of code?
What is the purpose of using a for loop, when nothing is varying w.r.t any variable?
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
"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.

Sign in to comment.

Answers (2)

Why nor just use Matlab's inbuilt cart2pol function?

1 Comment

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

Sign in to comment.

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 Comments

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.
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];

Sign in to comment.

Products

Release

R2023b

Asked:

on 2 Nov 2023

Commented:

on 2 Nov 2023

Community Treasure Hunt

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

Start Hunting!