Plotting multiple functions from data stored in arrays.

2 Ansichten (letzte 30 Tage)
Gurvir
Gurvir am 29 Jul. 2023
Beantwortet: Star Strider am 29 Jul. 2023
I need to generate two values in a single plot with the condition that whenever data array (corresponding to the p_array) becomes negative ,those negative values aren't plotted.
Below is the function that i use to generate data
function data = negativity_data()
arr = [];
p_array = linspace(0, 0.5, 500);
for p = p_array
neg = Negativity(horodecki(4.3,p,0.9));
arr = [arr; p neg];
end
data = arr;
end
Then i use data from this function into another function to plot
function f = negativity_plot()
d = negativity_data();
x = d(:,1);
y = d(:,2);
f = plot(x,y);
end
I similarly have another values
function data = realignment_data()
arr = [];
p_array = linspace(0, 0.5, 500);
for p = p_array
rea = 0.5*TraceNorm(Realignment(horodecki(4.3,p,0.9)))-0.5;
arr = [arr; p rea];
end
data = arr;
end
%And the plot function
function f = realignment_plot()
d = realignment_data();
x = d(:,1);
y = d(:,2);
f = plot(x,y);
end

Antworten (1)

Star Strider
Star Strider am 29 Jul. 2023
We do not have the arrays, however an approach using ‘logical indexing’ will probably work here —
x = linspace(0, 1, 5000).';
y = sin(2*pi*x) .* cos(2*pi*5*x);
figure
plot(x, y)
grid
title('Original')
Lv = y>0; % Logical Vector
figure
plot(x(Lv), y(Lv))
grid
title('Positive Values With Connecting Lines Plotted')
xnan = x;
ynan = y;
xnan(~Lv) = NaN;
ynan(~Lv) = NaN;
figure
plot(xnan, ynan)
grid
title('Positive Values Without Connecting Lines Plotted')
Since NaN values do not plot, the last figure has no lines connecting the positive segments.
.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by