Matlab 为矩阵中的每列制作多个直方图

Matlab 为矩阵中的每列制作多个直方图,matlab,Matlab,我想循环306X20矩阵中的每一列,在空白处填充NaN值,并且我想为每一行创建一个直方图(以20个直方图结束)。最好的方法是什么?我要实现的伪代码: For i = 1:(number of columns) % Loop through each column to generate a different histogram with the same % x and y labels and title % hist(data, 20) end 谢谢你我想你

我想循环306X20矩阵中的每一列,在空白处填充NaN值,并且我想为每一行创建一个直方图(以20个直方图结束)。最好的方法是什么?我要实现的伪代码:

For i = 1:(number of columns)
     % Loop through each column to generate a different histogram with the same
     % x and y labels and title
     % hist(data, 20)
end

谢谢你

我想你希望每个直方图都有一个单独的数字。这可以通过for循环和
figure
-语句轻松实现,以便在每次迭代中打开一个新的图。在Matlab版本R2014b及以上版本中,使用
直方图
-函数绘制直方图,在R2014b以下版本中,使用
hist
hist
仍适用于R2014b及以上版本)。这两个函数都忽略数据集中的值

% generate random data with NaN-values
x = randn(306,20);
a = randi(5,[306,20]);
x(a==3) = NaN;

% plot the histograms
for i = 1:size(x,2)
    figure;
    histogram(x(:,i)) % before R2014b use "hist" instead
    title(['Histogram of row ',num2str(i)]);
    xlabel('Bins');
    ylabel('Frequency');
end
这将为最后一行提供以下结果:

您可以使用和的组合。结果将是包含唯一值的单元格数组
values
,以及包含这些值出现次数的
count

A= [1 2 5; 1 7 NaN]; % // Test data

% // Convert it to a cell array so that we can apply a function to each row
B=mat2cell(A, [size(A,1)] ,[ones(size(A,2),1).']);

% // Find the unique values
[values,ia,indvalues]=cellfun(@unique,B,'UniformOutput',false);

% // Count the unique occurrencies.
count = cellfun(@(M) sum( bsxfun(@eq, M, unique(M)') )', B, 'UniformOutput',false);


>> values{1}

ans =

 1

>> count{1}

ans =

     2