How to put multiple elements in a cell?

22 views (last 30 days)
Nandini Chatterjee
Nandini Chatterjee on 10 Apr 2020
Edited: Nandini Chatterjee on 10 Apr 2020
I am loading in a mat file that contains a 6 X 191 cell (contains pixels).
*I am not able to upload a zip file of the mat file because of the size so I took a screenshot.
I want to calculate the mean, medican, std, skewness, and kurtosis for each column.
Then I want to take each of the column's statistic and put it in a array with 5x1 elements in each cell. (1 x 191 cell array with 5 elements in each cell)
Is that possible? In the future, I want to make a for loop out of this, so I can go through multiple mat files.
I have this code so far.
I tried using the vertcat and I ended up getting a 5 X 191 double (i don't know if it turned to a matrix).
% lead feats.mat
load('Feats.mat');
% calculate various statistics per column
feat_mean = arrayfun(@(k) mean([featints{1,k} ; featints{2,k}]), 1:size(featints,2));
feat_med = arrayfun(@(k) median([featints{1,k} ; featints{2,k}]), 1:size(featints,2));
feat_std = arrayfun(@(k) std([featints{1,k} ; featints{2,k}]), 1:size(featints,2));
feat_skw = arrayfun(@(k) skewness([featints{1,k} ; featints{2,k}]), 1:size(featints,2));
feat_kurt = arrayfun(@(k) kurtosis([featints{1,k} ; featints{2,k}]), 1:size(featints,2));
% combine all the stats into different rows
combine_feat_stat = vertcat(feat_mean, feat_med, feat_std, feat_skw, feat_kurt);
% 5X1 elements in each cell

Answers (1)

KSSV
KSSV on 10 Apr 2020
Let A be your 6*191 cell array.
[m,n] = size(A) ;
M = zeros(1,n) ; % initialize mean
for i = 1:n
C = cell2mat(A(:,i)) ; % convers the first column of cells into array
M(i) = mean(C) ;
end
  1 Comment
Nandini Chatterjee
Nandini Chatterjee on 10 Apr 2020
Edited: Nandini Chatterjee on 10 Apr 2020
Thank you but I don't think that will help with the 5x1 cell in each cell. Instead I get a class type of double [#;#;#;#;#]. How can I fix this?
I want this in the end,
Not this,
% lead feats.mat
load('Feats.mat');
% calculate various statistics per column
[m,n] = size(featints);
feat_mean = zeros(1,n); % initialize mean
feat_med = zeros(1,n); % initialize median
feat_std = zeros(1,n); % initialize standard deviation
feat_skw = zeros(1,n); % initialize skewness
feat_kurt = zeros(1,n); % initialize kurtosis
for i = 1:n
C = cell2mat(featints(:,i)); % convert the first column of cells into array
feat_mean(i) = mean(C);
feat_med(i) = median(C);
feat_std(i) = std(C);
feat_skw(i) = skewness(C);
feat_kurt(i) = kurtosis(C);
end
% combine all the stats into different rows
combine_feat_stat = vertcat(feat_mean, feat_med, feat_std, feat_skw, feat_kurt);
out = num2cell(combine_feat_stat,1);
% 5x1 elements in each cell [1xn cell array]
feat_statistics = num2cell(out,1);
feat_stats = {};
for k1 = 1:length(feat_statistics)
if ~isempty(feat_statistics{k1})
feat_stats = horzcat(feat_stats, feat_statistics{k1});
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!