How to plot an array of complex numbers using a For Loop and each point uses a different image/color.

조회 수: 4 (최근 30일)
I have a homework assignment that reads as such:
"Plot each complex number in the two problems above (12 dierent points) in MATLAB using a unique symbol/color combination for each point. Label the real and imaginary axes and provide a legend. Store all these complex numbers in a single array and use a for loop to make your plot. This means there will only be one line that uses the plot command, located inside the for loop."
I have my code written most of the way but I do not have a way to plot each point individually using a For Loop. I can plot them all as circles using:
For index = 12
plot (z,'o')
end
but I do not have a way with this method to plot unique points. All of the help sites tell me to use the Hold On command, or list z1, z2, z3... and plot it all as one thing, but I can only use the plot command once (so Hold on won't help) and I have to use an array (so plot (z1, 'o', z2, 's'...) won't help either.

답변 (1개)

Jan
Jan 2017년 8월 27일
Did you try to run you code? Start with a proper for loop:
for index = 1:12
Lower "for" and the loop does not run over the scalar 12, but from 1 to 12.
Now use the index inside the loop: z(index). If this is a complex number, how can you draw it to an x and y axis?
The documentation of plot explains, how to use specific colors and symbols. Maybe you want to use a cell string as a list of symbols. See also colormap to create a list of colors.
Finally the advice was correct: hold on helps to avoid, that each plot command clears the fromerly drawn objects.
  댓글 수: 2
jeremy morgan
jeremy morgan 2017년 8월 27일
So I tried doing what you said about making a character string, here is the code i used for the first 4 entries:
z = [2+5i; (1/3)+1i; 2+11i; 4+2i;];
C = ['o'; 'o'; 's'; 's'];
for index = 1:4
plot (z(index),C(index))
end
Now all i got when I hit run was a plot like this:
which is very confusing to troubleshoot because it lists the first entry in the array(z) but the final symbol entry in array(C). Am I telling it to pull from the index incorrectly?
Charlie Wannall
Charlie Wannall 2020년 2월 26일
Hey friend,
Make sure to have "hold on". It looks like all you are seeing is the last result as it just plotted the last point with the last linespec of 4 + 2i with "s" (a square) which implies it's writing over previous plots as it goes through the for loop.
Cheers

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

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by