Calculations on a particular data dimension, from a structure with varying dimensions per field

1 view (last 30 days)
I have a structure of parameters (part of a texture synthesis program), which contains 10 fields, each has sets of parameters with different dimensionality.
textStruct =
pixelStats: [30x6 double]
pixelLPStats: [30x5x2 double]
autoCorrReal: [4-D double]
autoCorrMag: [5-D double]
magMeans: [30x18 double]
cousinMagCorr: [4-D double]
parentMagCorr: [4-D double]
cousinRealCorr: [4-D double]
parentRealCorr: [4-D double]
varianceHPR: [1x30 double]
This structure is a sample of 30 sets of texture parameters. I am trying to conduct tests of normality for the first dimension of each (i.e. across the 30 samples), so that every value in every field has a test within its own group of 30 values. This is achieved by some simple functions by including a value for dims(example with mean and std below). Here is the example of using mean(x,dim) and std(x,flag,dim), and zscore(x,dist,dim) that is computed after converting my structure into a matrix.
FN = fieldnames(textStruct)
for i = 1:length(FN)
fn = FN{i};
% THIS BIT CONVERTS TO MAT
% for each field, convert input cell arrays into one (N+1)-dim matrix
ss = {textStruct.(fn)}; %input cell array of data
ssdim = ndims(ss{1}); % dimensions of the data
TT = cell2mat(reshape(ss,[ones(1,ssdim),sz])); %convert to matrix of appropriate dims
% PERFORM CALCULATIONS
M.(fn) = mean(TT,ndims(TT)); %gets sample mean for each cell
S.(fn) = std(TT,0,ndims(TT)); % get sample stdev
Z.(fn) = zscore(TT,0,ndims(TT)); % get zscores
%%something here along the lines of kstest(TT,ndims(TT)) is what I need!
end
Without the in-built functionality of the dims command, I am struggling to access the chunks of data that I require. I am using kstest for normality, and need to send it vectors of 30 data points, representing each value in textStruct across the 30 samples. I have racked my brain and the best solution I can come up with is to explicitly create loops for each field with the required depth according the ndims(current_field). I have a gut feeling that there is a better way of accomplishing this without hardcoding anything, but am totally lost!
I hope I have described my problem sufficiently, thanks for taking a look.
Alex

Accepted Answer

Guillaume
Guillaume on 25 Sep 2014
I believe the following should work:
TTcell = num2cell(TT, ndims(TT)); %each cell is the elements of TT in the last dimension
TTktest = cell2mat(cellfun(@(v) ktest(squeeze(v)), TTcell, 'UniformOutput', false));
  1 Comment
Alex Coningham
Alex Coningham on 25 Sep 2014
Thank you very much for your help here, I am self taught and have not really dabbled in cellfun.
This solution is simple and effective, while being clear to me as to its function.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!