Storage data in a harmonized matrix all values generated from a structure matrix with specific conditions

2 views (last 30 days)
Hello, I obtain a struct matrix than contains different results according to the number of iterations that was performed. Let us say that the number of iteration is 4 (n_i=4) for this example. So I obtain the "results" values in a structured matrix (A):
A(1x4 struct)
Inside the matrix, I have a single column with 4 rows that has the "results". I provide the following results of matrix A using 4 iterations as an example:
7x3 double
8x3 double
-1
-1
As we can see, the first two rows only have the data that will be used to generate a harmozised matrix with all the indexes and their respective results. The other two rows with "-1" indicates that only two iterations were needed and the third and fourth iterations are not needed as they do not provide any results.
Now, inside each structure of the matrix (the first-7x3 and second row-8x3 for this example), the first column contains the indexes of the location of each value and only the indexes different from 0 will be used to construct the harmonized matrix (as they do not provide any additional data in the other consecutive rows, see second matrix in the example below) . The values of the indexes from the first column will match the position to the data located in the other two columns (the second column is the current amplitude that has spike issues and the third column is the corrected amplitude of the signal where spikes were eliminated according to the neighbouring average).
The generation of this harmonized matrix (9x3 an example provided at the end) should be automatized according to the number of iterations, so only the matrices that contains data will be collected in a harmonized matrix. Sometimes, based on the number of iteration each structure matrix may duplicate a specific indexes (e.g. 368 as given as an example below). So, the harmonized matrix should select the lowest value from the second column with the respective value of the third column to avoid repeating the same index as shown in the harmonized matrix provided below.
The matrix 8x3 can have the following structure
158 -0.573800000000000 0.144935922330097
171 -0.515800000000000 -0.145098543689320
228 -1.22680000000000 -0.439054854368932
347 -0.636800000000000 -0.00694077669902918
352 0.903200000000000 0.390577901934952
358 -0.632800000000000 -0.0447915471908687
368 -0.753800000000000 -0.209547572815534
368 -0.653800000000000 -0.209547572815534
and the matrix 9x3 can have the following struncture
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
368 -0.653800000000000 -0.209547572815534
452 -0.618459223300971 -0.325464299874279
855 0.310200000000000 0.0439810936907452
the harmonized matrix will have the following structure (9x3)
158 -0.573800000000000 0.144935922330097
171 -0.515800000000000 -0.145098543689320
228 -1.22680000000000 -0.439054854368932
347 -0.636800000000000 -0.00694077669902918
352 0.903200000000000 0.390577901934952
358 -0.632800000000000 -0.0447915471908687
368 -0.653800000000000 -0.209547572815534
452 -0.618459223300971 -0.325464299874279
855 0.310200000000000 0.0439810936907452
I would appreciate any help.

Voss on 24 Jul 2023
Edited: Voss on 25 Jul 2023
Here's one way.
M1 = [ ...
158 -0.573800000000000 0.144935922330097; ...
171 -0.515800000000000 -0.145098543689320; ...
228 -1.22680000000000 -0.439054854368932; ...
347 -0.636800000000000 -0.00694077669902918; ...
352 0.903200000000000 0.390577901934952; ...
358 -0.632800000000000 -0.0447915471908687; ...
368 -0.753800000000000 -0.209547572815534; ...
368 -0.653800000000000 -0.209547572815534];
M2 = [ ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
368 -0.653800000000000 -0.209547572815534
452 -0.618459223300971 -0.325464299874279; ...
855 0.310200000000000 0.0439810936907452];
A = struct('results', {M1 M2 -1 -1})
A = 1×4 struct array with fields:
results
% make a copy of A (just so that A is unchanged by this process, in case that matters)
B = A;
% remove elements of B whose 'results' field is -1
B(arrayfun(@(x)isequal(x.results,-1), B)) = [];
% vertically concatenate the remaining 'results' fields
M = vertcat(B.results);
M(M(:,1) == 0, :) = [];
% apply the get_min function (defined below) to each group of rows in M.
% groups are defined by the values in the first column because of findgroups(M(:,1))
M_harmonized = splitapply(@get_min, M, findgroups(M(:,1)));
disp(M_harmonized);
158.0000 -0.5738 0.1449 171.0000 -0.5158 -0.1451 228.0000 -1.2268 -0.4391 347.0000 -0.6368 -0.0069 352.0000 0.9032 0.3906 358.0000 -0.6328 -0.0448 368.0000 -0.6538 -0.2095 452.0000 -0.6185 -0.3255 855.0000 0.3102 0.0440
% get_min gets the row with the minimum absolute value in column 2
function out = get_min(x)
[~,idx] = min(abs(x(:,2)));
out = x(idx,:);
end
3 CommentsShow 1 older commentHide 1 older comment
Voss on 25 Jul 2023
You're welcome! I've edited the answer to get the minimum absolute value.
Thank you very much.

Categories

Find more on Logical in Help Center and File Exchange

R2021a

Community Treasure Hunt

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

Start Hunting!