Plotting multiple functions from data stored in arrays.

조회 수: 2 (최근 30일)
Gurvir
Gurvir 2023년 7월 29일
답변: Star Strider 2023년 7월 29일
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

답변 (1개)

Star Strider
Star Strider 2023년 7월 29일
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.
.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by