How to plot multiple Loops and draw data lables for legend

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 comentarios

Loop not required:
C = (A').^B ;
How would like to add legend? Do you have any demo picture?
@KSSV, Thanks
C = (A').^B ; % It could be ( Not Always) dangerous and may result in Matrix dim mismatch
I welcome your plot proposal
You can redraw figure using Base or Exponent as X axis .
I expect legend with base and exponent info

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentarios

Makes lot of sense from your plot. May be i missed to explainn expectation in a better way.
I am expecting i and j values in the legend .
Calculated Power using base (i) and expononent (j) test vector.
Example
i = 4;
j = 5;
PowerC = sprintf (' base_%d_exponent_%d',i,j)
PowerC = ' base_4_exponent_5'
By using text function, labelling can be done to points
Updating the code provided before
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));
text(B(j),C(i,j),"bas-"+num2str(A(i))+",exp-"+num2str(B(j)));
%adds label to point having x-coordinate as first argument,
%y-coordinate as second argument and
%text to be displayed as third argument
if i==1 && j==1
hold on% holds the axes such that it contains previous plots and new plots
end
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
end
hold off % removes the "hold on" constraint
xlabel('Matrix-B');
ylabel('Matrix-C');
legend
Thanks ! Nice, apologies for being picky - legend text is overlapping for little bit large value. Could you please suggest non overlap text print method ?
To avoid text overlapping use annotation as mentioned here.
Now I can use your solution in my work.
Thanks a lot for wonderful help!!

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2021b

Preguntada:

el 24 de Sept. de 2021

Comentada:

el 28 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by