MATLAB Answers

Problem with applying a function (kstest) to cell arrays

3 views (last 30 days)
Dear all,
I want to have the result of kstest (Kolmogorov-Smirnov test) for all arrays in a cell. In fact, I have a 1x93 cell that contains 93 doubles and the third column of these doubles is the column that I want to calculate kstest for. I searched and write this script:
for i=1:length(C1)
[h, p, k2stat] = arrayfun(@kstest, C1{1,i}(:, end), 'UniformOutput', false);
end
Although it runs successfully and the output is h with 336x1; I believe it gives me the wrong answer cause when I manually check some cell arrays it gives me the different answer (in some cases the output of my script reject null hypothesis while when I manually check it using this code:
kstest(C1{1,27}(:,end))
it's accepts the null hypothesis. So please let me what is my mistake if you know. I attached a small sample.
Thank you in advance

  0 Comments

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 29 May 2020
The issue I see is you are using arrayfun, which applies the function to each element in the vector as opposed to each cell.
My recommendation is to use cellfun instead.
[h, p, k2stat] = cellfun(@(c)kstest(c(:,end)), C1);
On the other hand, you could also do this manually using a for loop.
for i=1:length(C1)
[h(i), p, k2stat] = kstest(C1{i}(:, end));
end

  0 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 May 2020
Is this what you're looking for?
s = load('C.mat')
C1 = s.C
numCells = length(C1)
for k = 1 : length(C1)
thisArray = C1{k}; % Extract 2-D matrix.
thisArray = thisArray(:, 3); % Extract only column 3.
% Do kstest on column 3 only:
k2stats(k) = kstest(thisArray);
% [h, p, k2stat] = arrayfun(@kstest, C1{1,i}(:, end), 'UniformOutput', false);
% Visualize it
subplot(4, 2, k);
plot(thisArray, 'b.-');
grid on;
caption = sprintf('Array #%d', k);
title(caption, 'FontSize', 18);
end
subplot(4, 2, 8);
bar(k2stats)
title('k2stats', 'FontSize', 18);
k2stats % Show in command window
See the link on Loren Shure's blog for a discussion:

  0 Comments

Sign in to comment.

Products


Release

R2020a

Translated by