MATLAB Answers


Can't take the mean of a 2D matrix (Array indices must be positive integers or logical values.)

Asked by Mike Bakeman on 6 Aug 2019
Latest activity Commented on by Rik
on 8 Aug 2019
I have a 3D data set that I'm trying to take the mean and std along a single dimension and then average the mean or the std along the other two dimensions. Once I get the mean or the std along one dimension I have a 2d matrix. I can take the mean of the first matrix (means), but I can't with the second 2d matrix (STDs) The error I get is
Array indices must be positive integers or logical values.
This should be really simple.
%get rid of headers
data = data_set(6:2165,1:2048,1:16);
%permute data putting temporal frames first
data_frames_first = permute(data,[3,1,2]);
%average all the frames
frames_avg = mean(data_frames_first);
%permute frames back to last
frames_avg_and_last = permute(frames_avg,[2,3,1]);
%average all of our 2160 rows
frames_rows_avg = mean(frames_avg_and_last);
%finally average vertically along our 2048 columns
mean = mean(frames_rows_avg)
%take std of all frames element by element
frames_std = std(data_frames_first);
%permute frames back to last
frames_std_and_last = permute(frames_std,[2,3,1]);
%average all of our 2160 rows of stds
frames_stds_rows_avg = mean(frames_std_and_last);
This where I get the error
Error (line 29)
frames_stds_rows_avg = mean(frames_std_and_last);
Array indices must be positive integers or logical values.


Sign in to comment.

1 Answer

Answer by Rik
on 6 Aug 2019
 Accepted Answer

You are using mean both as a function name and a variable name.
Also, if you read the doc for mean, you will notice you can explicitly set the dimension to be reduced. If I recall correctly the same is true for std.


You're welcome. If my answer solved your problem, please consider marking it as accepted answer. If not, feel free to comment with your remaining issues.

Sign in to comment.