# How to plot biggest positive and negative difference as a lines?

19 ビュー (過去 30 日間)
Amund Romseland 2023 年 12 月 31 日

I'm trying to recreate a graph that includes the lines for the biggest positive and negative lines but I'm completely stuck on something I feel should be pretty simple. Im trying to recreate the thick blue and green line from this graph from a1 = diff(a) and max(a1)/min(a1).
This is the code I'm supposed to use but after searching and trying for hours I'm starting to give up. Please don't give me the answer and instead give some hints or help to help me figure it out on my own.
a1 = diff(a);
[a1_max , x_max] = max(a1);
plot(x_max,a1_max,'b*','MarkerSize',12)
[a1_min ,x_min ] = min(a1);
plot(x_min,a1_min,'g*','MarkerSize', 12)
% This is where I'm getting stuck. Cant figure out what to put in where
% there are two ".."
x_pos = x_max:..;
plot(x_pos , a(..),'b','LineWidth',3)
x_neg = ..;
plot(x_neg , a(..),'g','LineWidth',3)
##### 2 件のコメントなしを表示なしを非表示
Dyuman Joshi 2023 年 12 月 31 日

Hint - When you calculate the difference using diff(), the result is an array with an element less than the original array.
So you have to take into account for the corresponding elements, as difference is calculated using 2 values.
a1 = diff(a);
%Note, a1(k) = a(k+1) - a(k)
% Find the indices corresponding to maximum and minimum differences
% Get the values if they are required for further computation
[~, x_max] = max(a1);
[~, x_min] = min(a1);
%Indices of elements that make up the largest negative difference
%The current minimum index and the previous one
idxmix = x_min + [-1 0]
%Same for the largest positive difference
%The current positive index and the previous one
idxmax = x_max + [-1 0]
Now you can use these indices to plot the corresponding lines.
Dyuman Joshi 2024 年 1 月 1 日
Did you check the hints given below?

サインインしてコメントする。

### 採用された回答

Hassaan 2023 年 12 月 31 日
As per recommendation from @Dyuman Joshi [special thanks] hope this will do the job.
% Define a dummy signal based on the figure (replace this with actual data)
a = rand(1, 50) * 4 - 2; % Generating a random signal for illustration
% Compute the first discrete difference of the signal
a1 = diff(a);
% Find the indices and values of the largest point-to-point positive and negative differences
[a1_max, x_max_diff] = max(a1);
[a1_min, x_min_diff] = min(a1);
% Indices of the original signal that contribute to the max and min differences
idx_max = x_max_diff + [0 1]; % The point before and the point of max difference
idx_min = x_min_diff + [0 1]; % The point before and the point of min difference
% Plot the original signal
plot(a, 'b-o');
hold on; % Keep the plot for adding new elements
% Plot the lines for the largest point-to-point differences
% Using idx_max and idx_min to refer to the correct points in 'a'
plot(idx_max, a(idx_max), 'g', 'LineWidth', 3);
plot(idx_min, a(idx_min), 'r', 'LineWidth', 3);
% Enhance the plot
xlabel('Index k');
ylabel('Signal value');
title('Signal a with Largest Point-to-Point Differences');
legend('Signal a', 'Largest Rise', 'Largest Drop');
ylim([min(a) - 1, max(a) + 1]);
grid on; % Add a grid for better visibility
% Hold off the plot to prevent more plotting on the same figure
hold off;
In this updated code:
• idx_max and idx_min are used to find the correct indices in the original signal array a that correspond to the largest point-to-point increase and decrease.
• These indices are then used to plot the points and lines that illustrate the largest rise and drop in the signal.
• The plot commands for the point-to-point differences now correctly reference these adjusted indices.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

サインインしてコメントする。

### その他の回答 (2 件)

Hassaan 2023 年 12 月 31 日

An initial implementation to give you the idea. Incase of any issue let me know.
% Define a dummy signal based on the figure (replace this with actual data)
a = rand(1, 50) * 4 - 2; % Generating a random signal for illustration
% Compute the first discrete difference of the signal
a1 = diff(a);
% Find the indices and values of the largest point-to-point positive and negative differences
[a1_max, x_max_diff] = max(a1);
[a1_min, x_min_diff] = min(a1);
% Find the indices and values of the overall maximum and minimum relative to the first point
[overall_max, x_max_overall] = max(a - a(1));
[overall_min, x_min_overall] = min(a - a(1));
% Plot the original signal
plot(a, 'b-o');
hold on; % Keep the plot for adding new elements
% Plot the lines for the largest point-to-point differences
plot([x_max_diff, x_max_diff], [a(x_max_diff), a(x_max_diff+1)], 'g', 'LineWidth', 3);
plot([x_min_diff, x_min_diff], [a(x_min_diff), a(x_min_diff+1)], 'r', 'LineWidth', 3);
% Plot the lines for the overall largest rise and drop
plot([x_max_overall, x_max_overall], [a(1), overall_max + a(1)], 'g--', 'LineWidth', 3);
plot([x_min_overall, x_min_overall], [a(1), overall_min + a(1)], 'r--', 'LineWidth', 3);
% Enhance the plot
xlabel('Index k');
ylabel('Signal value');
title('Signal a with Largest Differences');
legend('Signal a', 'Largest Point-to-Point Rise', 'Largest Point-to-Point Drop', 'Overall Largest Rise', 'Overall Largest Drop');
ylim([min(a) - 1, max(a) + 1]);
grid on; % Add a grid for better visibility
% Hold off the plot to prevent more plotting on the same figure
hold off;
This script will create a plot with a dummy signal and will highlight the largest positive and negative differences with vertical lines. The signal a is randomly generated for the purpose of this example; you should replace it with your actual signal data. The ylim function is set dynamically to ensure that the vertical lines are well within the plot range. The grid on command adds a grid to the plot, which can help in visual analysis.
Also, adjust the ylim if needed to ensure that the lines are within the visible range of your plot. The xlabel, ylabel, and title functions are used to label the x-axis, y-axis, and the plot itself, respectively. The legend function adds a legend to help identify the plot elements
• a1_max and a1_min represent the largest point-to-point rise and drop.
• overall_max and overall_min represent the largest overall rise and drop from the first data point.
• Solid lines ('g' and 'r') represent the point-to-point differences.
• Dashed lines ('g--' and 'r--') represent the overall rise and drop from the first point.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Hassaan 2023 年 12 月 31 日
@Dyuman Joshi Updated my logic hopefully it is accurate now.
Dyuman Joshi 2023 年 12 月 31 日
It does not seem to be correct, compare the figure obtained from your code to the output OP expects.

サインインしてコメントする。

Adam Danz 2023 年 12 月 31 日
I believe the task is to create two lines, one that connects the biggest increase and one the connects the biggest decrease between consecutive points along a line.
1. Use diff to compute the differenc between consecutive values.
2. Locate the largest positive difference and largest negative difference
3. Use the index of those values to get the coordinates of the original values
4. Plot the two lines.
% Create demo values
a = 2*randn(1,50);
x = 1:numel(a);
tiledlayout(2,1)
ax1 = nexttile();
plot(x,a,'-bo','DisplayName','Data')
title('data')
% Step 1
da = [nan,diff(a(:)')]; % pad with one NaN to maintain indexing
ax2 = nexttile();
plot(x,da, '-r^')
yline(0)
title('\Delta data')
% Step 2
[~, posIdx] = max(da);
[~, negIdx] = min(da);
% Step 3
maxIncreaseX = posIdx-[1,0];
maxDecreaseX = negIdx-[1,0];
maxIncreaseValues = a(posIdx-[1,0]);
maxDecreaseValues = a(negIdx-[1,0]);
% Step 4
hold(ax1, 'on')
plot(ax1, maxIncreaseX, maxIncreaseValues, 'r-', 'LineWidth', 2, 'DisplayName', 'MaxIncrease')
plot(ax1, maxDecreaseX, maxDecreaseValues, 'g-', 'LineWidth', 2, 'DisplayName', 'MaxDecrease')
legend(ax1,'Location','EastOutside')

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeBartlett についてさらに検索

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by