- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
How to split a matrix of absolute value in min and max?
1 view (last 30 days)
Show older comments
Dear all,
Unfortunately, I have not found an answer to my question in the forum, which is why I would like to address the following question to you:
I have a matrix A=(10,1,10000) where 10 is the number of periods t. I would like to make 3 plots. In the first plot I want to show in which periods the 10,000 draws reach their peak, regardless of the sign, but in absolute value. To do this, I proceeded as follows:
X=abs(A(10,1,10000));
[ii,jj]=max(X);
N=accumarray(jj(:),1).';
This gives me a plot where I have t=1:10 on the x axis and the summed number of peaks for each t is shown on the y axis. But now I want two more plots in which I do the same again but take the sign into account. That is, in one plot I want to show the number of peaks in each period when the peak in ii is a maximum and in the other plot I want to show the number of peaks in each period when the peak in ii is a minimum.
Unfortunately, I always get a Min Matrix B = (1,1,10000) and a Max Matrix C=(1,1,10000). But the 10,000 entries should be split into min and max, e.g. B = (1,1,3500) and C=(1,1,6500);
Can someone tell me how to do this?
Many thanks for your help!
0 Comments
Accepted Answer
Hassaan
on 19 Jan 2024
% Dummy data for A
A = randn(10,1,10000); % Normally distributed random numbers
% Find absolute peaks across the third dimension
X = abs(A);
[peakValues, peakIndices] = max(X,[],1); % peakIndices will have the periods of the peaks
% Reshape peakIndices to be a row vector
peakIndices = reshape(peakIndices, 1, []);
% Find total peaks
totalPeaks = accumarray(peakIndices(:), 1, [10, 1])';
% Determine if each peak is a maximum (positive) or minimum (negative)
% Adjust the indices for the original values
originalPeaks = A(sub2ind(size(A), peakIndices, ones(1,10000), 1:10000));
% Prepare indices for positive and negative peaks
positivePeakIndices = peakIndices(originalPeaks > 0);
negativePeakIndices = peakIndices(originalPeaks < 0);
% Check if there are positive and negative peaks and handle accordingly
if ~isempty(positivePeakIndices)
positivePeaks = accumarray(positivePeakIndices(:), 1, [10, 1])';
else
positivePeaks = zeros(1, 10);
end
if ~isempty(negativePeakIndices)
negativePeaks = accumarray(negativePeakIndices(:), 1, [10, 1])';
else
negativePeaks = zeros(1, 10);
end
% Plotting
figure;
% Plot total peaks
subplot(3,1,1);
bar(1:10, totalPeaks);
title('Total Number of Peaks per Period');
xlabel('Period');
ylabel('Number of Peaks');
% Plot positive peaks
subplot(3,1,2);
bar(1:10, positivePeaks);
title('Number of Positive Peaks per Period');
xlabel('Period');
ylabel('Number of Positive Peaks');
% Plot negative peaks
subplot(3,1,3);
bar(1:10, negativePeaks);
title('Number of Negative Peaks per Period');
xlabel('Period');
ylabel('Number of Negative Peaks');
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.
Professional Interests
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
3 Comments
Hassaan
on 19 Jan 2024
% Dummy data for A
A = randn(10,1,10000); % Normally distributed random numbers
% Find absolute peaks across the third dimension
X = abs(A);
[absPeakValues, peakIndices] = max(X,[],1); % peakIndices will have the periods of the peaks
% Reshape peakIndices to be a row vector
peakIndices = reshape(peakIndices, 1, []);
% Find the actual values at the peak indices
actualPeakValues = A(sub2ind(size(A), peakIndices, ones(1,10000), 1:10000));
% Find total peaks
totalPeaks = accumarray(peakIndices(:), 1, [10, 1])';
% Separate the indices of the actual positive and negative peaks
actualPositivePeakIndices = peakIndices(actualPeakValues > 0);
actualNegativePeakIndices = peakIndices(actualPeakValues < 0);
% Count the occurrences of actual positive and negative peaks
actualPositivePeaks = accumarray(actualPositivePeakIndices(:), 1, [10, 1], [], 0)';
actualNegativePeaks = accumarray(actualNegativePeakIndices(:), 1, [10, 1], [], 0)';
% Plotting
figure;
% Plot total peaks
subplot(3,1,1);
bar(1:10, totalPeaks);
title('Total Number of Peaks per Period');
xlabel('Period');
ylabel('Number of Peaks');
% Plot positive peaks
subplot(3,1,2);
bar(1:10, actualPositivePeaks);
title('Number of Actual Positive Peaks per Period');
xlabel('Period');
ylabel('Number of Positive Peaks');
% Plot negative peaks
subplot(3,1,3);
bar(1:10, actualNegativePeaks);
title('Number of Actual Negative Peaks per Period');
xlabel('Period');
ylabel('Number of Negative Peaks');
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!