フィルターのクリア

Remove NAN from cell array, and calculate the average of each column through each cell

2 ビュー (過去 30 日間)
F Z
F Z 2015 年 4 月 22 日
コメント済み: F Z 2015 年 4 月 23 日
I have a 1 x 17 cell array called M, with each cell containing a 16 x 27 Matrix. I need to get rid of NaN in each cell. I tried this code:
for i=1:length(M) ind = isnan(M{1}); M{i}(ind)=[]; end
But the cells are reshaped to rows (1x240) and I need the expected result to be a p*q Matrix. Once the expected result is obtained, I have to go through each cell and calculate the average of each column q Any help please?
  2 件のコメント
Punit
Punit 2015 年 4 月 22 日
First of all
ind = isnan(M{1});
should be ind = isnan(M{i});
Also, try replacing M{i}(ind) = 0;
then sum(M(:,q))/length(find(~ind(:,q))) would be the average of column q
Guillaume
Guillaume 2015 年 4 月 22 日
編集済み: Guillaume 2015 年 4 月 22 日
What does 'get rid of NaN' mean? Obviously, you need something in that matrix entry where the NaN is: a number, NaN, Inf or +Inf are the only possibilities.
If you remove the matrix element, then the matrix cannot be square anymore.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2015 年 4 月 22 日
As of version 2015a, mean can ignore NaNs (in earlier version you had to use nanmean in the stat toolbox).
So the simplest way to calculate the mean is with:
colmeans = cellfun(@(m) mean(m, 'omitnan'), M, 'UniformOutput', false);
celldisp(colmeans);
Even simpler, would be to just store all your matrices as a 3D matrix (of size 16 x 27 x 17) and avoid cell arrays entirely:
M = cat(3, M{:});
colmeans = mean(M, 'omitnan')
  3 件のコメント
Guillaume
Guillaume 2015 年 4 月 22 日
A simpler version of your code would be:
T = cat(3, M{:});
colmeans = mean(T(4:9, 2:22));
F Z
F Z 2015 年 4 月 23 日
brilliant!! Thx a lot

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by