使用Matlab查找具有数字和字符串的数组的均值 [英] Find mean of an array with both numbers and strings with Matlab

查看:101
本文介绍了使用Matlab查找具有数字和字符串的数组的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个同时包含数字和字符串的单元格数组(arr_new),我想使用Matlab查找每列的平均值(并忽略字符串,因为这些是我想在计算中忽略的点) .该数组是一个200x200的单元格数组,它是数字和字符串的组合.

I have a cell array (arr_new) that includes both numbers and strings and I want to find the mean value of each column (and ignoring the strings because those are points that I want to ignore in my calculation) using Matlab. The array is a 200x200 cell array and it is a combination of numbers and strings.

我试图用这个:

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

但是,当然,由于字符串的原因,它不起作用.

But of course, it did not work because of the strings.

任何帮助将不胜感激.

推荐答案

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

这里有两个技巧.一种是使用cellfun,另一种是cell2mat.

There are two tricks here. One is the use of cellfun, and the other is cell2mat.

这篇关于使用Matlab查找具有数字和字符串的数组的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆