4D data /Index exceed array bounds
2 views (last 30 days)
Show older comments
Hey all,
I'm wrting a code which needs to store 2D position and velocity data in 4D cell arrays. I have to for loops first one is for each trial (k=1:5 which is the 4th dim), second one is for each words (l=1:134) which is the 3rd dim. I need to get the mean of data for each word over 5 trials and write a .dat file. In the tecplot section I got :
M_Vel(1,2,nsyll{k},k) Index in position 2 exceeds array bounds (must not exceed 1).
but my M_Vel is :
val(:,:,1,1) = {21600×4×135×2 double}
I couldn't fix it. If you're able to hepl me I'll be appreciate. Thanks in advance
1 Comment
DGM
on 5 Jul 2021
You might want to include some example data that can allow the problem to be reproduced.
Answers (3)
Are Mjaavatten
on 6 Jul 2021
Edited: Are Mjaavatten
on 6 Jul 2021
I think you may have inadvertently created a cell array containing your whole data matrix in element 1. Something like this:
>> M_vel = {rand([2,3,4,5])};
>> M_vel(:,:,2,1)
Index in position 3 exceeds array bounds (must not exceed 1).
To check this, type:
size(M_vel)
My guess is that you want a 4D array of doubles instead of a cell array. You may fix this by
M_vel = M_vel{1};
but a better idea is to look at your code and make sure that you make an array of doubles to start with. In my example:
>> M_vel = rand([2,3,4,5]);
>> M_vel(:,:,2,1)
ans =
0.4294 0.2976 0.1192
0.2573 0.4249 0.4951
Are Mjaavatten
on 7 Jul 2021
I think it may be better to let M_Val be a cell array of 5 ordinary arrays, where M_Val{k} is of dimension 134 by 2 by nsyll(k). This creates a demo version with random numbers:
n_trials = 5;
nsyll = [6,4,5,8,7,9];
n_words = 134;
M_Vel = cell(1,n_trials);
for k = 1:n_trials
M_Vel{k} = randn(n_words,2,nsyll(k));
end
You say want the mean of data for each word, averaged over the 5 trials. The data you average must have fixed dimensions, independent of k. One example might be to calculate the the min,max and variance for position and velocity for each given trial and word. To prepare for the averaging over trials, I suggest to collect these vaues in a 5 by 134 by 2 by 3 array of doubles:
results = zeros(n_trials,n_words,2,3);
for k = 1:n_trials
for j = 1:n_words
results(k,j,:,:) = [min(M_Vel{k}(j,:,:),[],3)',max(M_Vel{k}(j,:,:),[],3)'...
,var(M_Vel{k}(j,:,:),[],3)'];
end
end
Average over trials by taking the mean (over dimension 1) and remove the trials dimension by the squeeze command:
averages = squeeze(mean(results));
size(averages)
ans =
134 2 3
It is easy to get a little lost when handling multidimensional arrays, but I hope this is close enough to what you want, or that it may serve a starting point.
If you need more help, I shall need (a subset of) your data files.
0 Comments
mehtap agirsoy
on 7 Jul 2021
1 Comment
Are Mjaavatten
on 8 Jul 2021
I had a look at your Archive.zip file and I am unable to figure out what you are trying to do. My main impression is that you are trying to do way too much at once. For large, complex projects like this, the strategy is to divide and conquer! Try to split your project into (much) smaller parts and test that each part works and does what you intend. Then combine these building blocks a few at a time, always testing that you obtain the expected results.
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!