2017-10-28 15 views
1

数値と文字列の両方を含むセル配列(arr_new)があります。各列の平均値を求めたいと思います。 Matlabを使って私の計算で無視したい)。配列は200x200セル配列であり、数値と文字列の組み合わせです。Matlabで数値と文字列の両方を持つ配列の平均を求める

私はこれを使用してみました:

for k = 1:cols 
    Y(k) = mean(arr_new(k,:)); 
end 

をしかし、もちろん、それは理由の文字列で動作しませんでした。

ご協力いただければ幸いです。

答えて

1
nCols = size(arr_new,2); 
Y = nan(1, nCols); % pre-allocate 
for k = 1:nCols 
    isNum = cellfun(@isnumeric, arr_new(:,k)); % find number in the column 
    Y(k) = mean(cell2mat(arr_new(isNum,k))); % convert to mat for mean 
end 

ここに2つのトリックがあります。 1つはcellfunの使用であり、もう1つはcell2matです。

+0

ありがとうございました。それは本当にうまくいった。 –

2

あなたはNaNに文字列/文字を変換し、MATLAB R2015a (or later)または統計と機械学習ツールボックスのいずれかを持っているし、cell to a matrixを変換した後、それらの値を無視して平均値を求めることができる場合。

k = cellfun(@isnumeric, arr_new);  %finding the indices of the numeric values 
arr_new(~k)={NaN};      %replacing the rest of the indices with NaN 
%finding mean ignoring NaNs (which were chars/strings before) 
Y = mean(cell2mat(arr_new),'omitnan'); %for MATLAB R2015a or later 
% Use the following instead if you have R2014b or earlier with Stats and ML Toolbox: 
% Y = nanmean(cell2mat(arr_new)); 
+0

ありがとうございます!私はそれを試したときに成功しました! –

関連する問題