Borrar filtros
Borrar filtros

How to crop matrices at the maximal non-NaN values and then center the data

6 visualizaciones (últimos 30 días)
Hi all,
I am new to MATLAB and I have the following problem:
I have matrices something like:
H(1).matrix = [1 2 3
3 4 5
3 4 4
6 NaN 7
NaN NaN NaN]
H(2).matrix = [3 4 5
5 6 7
4 4 4
NaN 3 3
NaN 4 4
NaN NaN 7]
H(3).matrix = [3 3 3
2 1 3
NaN 1 2]
Now, I see that these matrices all have 3 columns but different row number, first is 5x3, second 6x3, third 3x3, but also within columns they have different row length with actual numbers and not NaN. I see that the longest rows length where each columns have non NaN values is 2 (in the third matrix, the first row and the second row with all non NaN values). So now I want to crop all the matrices to have size 2x3. Then I want to center all these matrices.
Something like:
H(1).matrix = [1 2 3
3 4 5]
H(2).matrix = [3 4 5
5 6 7]
H(3).matrix = [3 3 3
2 1 3]
Then I want to center the data (remove from each element the average of the column it belongs to).
Thank you in advance!

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 20 de Abr. de 2019
Editada: Andrei Bobrov el 21 de Abr. de 2019
M = struct2cell(H);
n = min(cellfun(@(x)find(any(isnan(x),2),1,'first')-1,M));
for jj = 1:numel(H)
H(jj).matrix = H(jj).matrix(1:n,:);
end
or without loop 'for-end'
M = struct2cell(H);
n = min(cellfun(@(x)find(any(isnan(x),2),1,'first')-1,M));
C = cellfun(@(x)x(1:n,:),M(:),'un',0);
H = cell2struct(C,'matrix',2);
  7 comentarios
Andrei Bobrov
Andrei Bobrov el 21 de Abr. de 2019
In your data in all matrices, the value of the 57th column is nan.
Spresi
Spresi el 21 de Abr. de 2019
Editada: Spresi el 21 de Abr. de 2019
Oh sorry, I did not notice that at all. It works now.. Thank you!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by