()-indexing must appear last in an index expression
1 view (last 30 days)
Show older comments
I have a 70x70x328 matrix and want to take the mean along either the first or second dimension (they're the same) while ignoring elements that are 0. I'm getting the error: ()-indexing must appear last in an index expression but don't know how to rearrange or what to add to my expression. Here's what I have
for i=1:length(files)
Mean1(:,i)=mean(All_files(:,:,i)(All_files(:,:,i)~=0),2);
end
Thanks!
0 Comments
Accepted Answer
Andrei Bobrov
on 1 Oct 2011
EDIT
Mean1 = squeeze(sum(data)./sum(data~=0))%first dimension
Mean2 = squeeze(sum(data,2)./sum(data~=0,2))%second dimension
0 Comments
More Answers (1)
Image Analyst
on 1 Oct 2011
See if this works for you:
m = randi(9, [70,70,328])-1; % 70x70x328
sumAlongDim2 = squeeze(sum(m, 2)); % 70x328
binaryVolume = m ~= 0; % 70x70x328
countAlongDim2 = squeeze(sum(binaryVolume, 2)); % 70x328
% Compute the average.
meanAlongDim2 = sumAlongDim2 ./ countAlongDim2;
imagesc(meanAlongDim2);
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!