"Invalid use of operator." doesn't go even after trying various solutions :(
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 2 de Nov. de 2023
"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.
Respuestas (2)
Steven Lord
el 2 de Nov. de 2023
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 comentarios
Steven Lord
el 2 de Nov. de 2023
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];
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!