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

3 ビュー (過去 30 日間)
Spresi
Spresi 2019 年 4 月 20 日
編集済み: Spresi 2019 年 4 月 21 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by