How to plot multiple Loops and draw data lables for legend

조회 수: 2 (최근 30일)
Hi
I want to plot return value C and add legend according to iteration for below code
A = [1:4];
B = [1:4];
C = zeros(size(A,2),size(B,2));
for i = 1:length(A)
for j = 1:length(B)
C(i,j) = power(A(i),B(j));
end
end
  댓글 수: 3
Life is Wonderful
Life is Wonderful 2021년 9월 24일
편집: Life is Wonderful 2021년 9월 24일
@KSSV, Thanks
C = (A').^B ; % It could be ( Not Always) dangerous and may result in Matrix dim mismatch
I welcome your plot proposal
Life is Wonderful
Life is Wonderful 2021년 9월 24일
You can redraw figure using Base or Exponent as X axis .
I expect legend with base and exponent info

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

채택된 답변

Shanmukha Voggu
Shanmukha Voggu 2021년 9월 27일
Hi,
I understood that you want to
1) plot every row of matrix C against matrix B
2) And label every row of matrix C individually using legend
This can be achieved by DisplayName property of the plot as shown below
A = [1:4];
B = [1:4];
C = zeros(size(A,2),size(B,2));
for i = 1:length(A)
for j = 1:length(B)
C(i,j) = power(A(i),B(j));
end
plot(B,C(i,:), 'DisplayName', "row - "+num2str(i));
% The DisplayName property is set to the row number of matrix C
% We can Customize the fourth argument above
if i==1
hold on % holds the axes such that it contains previous plots and new plots
end
end
hold off % removes the "hold on" constraint
xlabel('Matrix-B');
ylabel('Matrix-C');
legend
Refer to this for more information.
  댓글 수: 5
Shanmukha Voggu
Shanmukha Voggu 2021년 9월 28일
To avoid text overlapping use annotation as mentioned here.
Life is Wonderful
Life is Wonderful 2021년 9월 28일
Now I can use your solution in my work.
Thanks a lot for wonderful help!!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by