필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
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!

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by