Storage data in a harmonized matrix all values generated from a structure matrix with specific conditions
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jorge Luis Paredes Estacio
am 24 Jul. 2023
Kommentiert: Jorge Luis Paredes Estacio
am 25 Jul. 2023
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.
0 Kommentare
Akzeptierte Antwort
Voss
am 24 Jul. 2023
Bearbeitet: Voss
am 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})
% 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);
% remove the rows that start with a 0
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);
% 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!