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

4 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 20 Apr. 2019
Bearbeitet: Andrei Bobrov am 21 Apr. 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 Kommentare
Andrei Bobrov
Andrei Bobrov am 21 Apr. 2019
In your data in all matrices, the value of the 57th column is nan.
Spresi
Spresi am 21 Apr. 2019
Bearbeitet: Spresi am 21 Apr. 2019
Oh sorry, I did not notice that at all. It works now.. Thank you!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by