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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by