フィルターのクリア

Find peak values and locations in two matrices containing zeros

2 ビュー (過去 30 日間)
Askeladden2
Askeladden2 2024 年 1 月 18 日
回答済み: Hassaan 2024 年 1 月 18 日
Dear All Community Members,
I have two matrices, 'A' and 'B'. Matrix 'A' represents the time history of velocities, while matrix 'B' represents the corresponding times. I want to extract all the peak velocity values from matrix 'A' that are above a threshold value of 3.
Matrix A is as follows:
A=[ 1 3 -1 1 5 -1 0;1 6 3 -2 0 0 0;2 3 9 -2 1 11 -1;4 1 -2 8 5 0 0;2 -2 6 -1 0 0 0;-1 13 -2 0 0 0 0];
Matrix B is as follows:
B=[1 1.5 2.5 3 3.5 4 0;1.5 3 5 6 0 0 0;0.5 2 2.5 2.75 3 4 5;2 4 4.25 4.5 6 0 0;1 2 3 4 0 0 0;0.5 3 4 0 0 0 0];
Specifically, I am looking to extract the value 5 from the first row, the value 6 from the second row, the values 9 and 11 from the third row, and then values 8,6,13 from the from the fourth and to the last row in matrix A, as well as their locations.
I.e. Peak values: 5,6,9,11,8,6,13 at locations: 3.5, 3, 2.5, 4, 4.5, 3, 3.
I have tried the findpeaks function, but I am getting errors because of the ending zeros in matrix B.
Stephen23, gave me a brilliant tips in order to find the peaks using cells.
F = @(a)findpeaks(a,'Threshold',3);
C = cellfun(F,num2cell(A,2),'uni',0)
Is there a way I can do this without using cells? For example, using matrices and extending the rows with zeros.
I.e. Peak values, C=[5 0;6 0;9 11;8 0;6 0;13 0];
Locations, D=[3.5 0;3 0; 2.5 4;4.5 0; 3 0;3 0];
Thank you in advance.

採用された回答

Hassaan
Hassaan 2024 年 1 月 18 日
A = [1 3 -1 1 5 -1 0; 1 6 3 -2 0 0 0; 2 3 9 -2 1 11 -1; 4 1 -2 8 5 0 0; 2 -2 6 -1 0 0 0; -1 13 -2 0 0 0 0];
B = [1 1.5 2.5 3 3.5 4 0; 1.5 3 5 6 0 0 0; 0.5 2 2.5 2.75 3 4 5; 2 4 4.25 4.5 6 0 0; 1 2 3 4 0 0 0; 0.5 3 4 0 0 0 0];
threshold = 3;
C = []; % Matrix for peak values
D = []; % Matrix for locations
for i = 1:size(A, 1)
peaks = []; % Temp array for peaks in current row
times = []; % Temp array for times in current row
for j = 1:size(A, 2)
if A(i, j) > threshold && (j == 1 || A(i, j) > A(i, j-1)) && (j == size(A, 2) || A(i, j) > A(i, j+1))
peaks = [peaks, A(i, j)];
times = [times, B(i, j)];
end
end
% Padding with zeros to match the longest row
maxLen = max(size(C, 2), length(peaks));
C(i, 1:maxLen) = [peaks, zeros(1, maxLen - length(peaks))];
D(i, 1:maxLen) = [times, zeros(1, maxLen - length(times))];
end
% Display the results
disp('Peak Values:');
Peak Values:
disp(C);
5 0 6 0 9 11 4 8 6 0 13 0
disp('Locations:');
Locations:
disp(D);
3.5000 0 3.0000 0 2.5000 4.0000 2.0000 4.5000 3.0000 0 3.0000 0
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParametric Spectral Estimation についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by