4D data /Index exceed array bounds

조회 수: 3 (최근 30일)
mehtap agirsoy
mehtap agirsoy . 2021년 7월 5일
댓글: Are Mjaavatten . 2021년 7월 8일
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
DGM 2021년 7월 5일
You might want to include some example data that can allow the problem to be reproduced.

댓글을 달려면 로그인하십시오.

답변 (3개)

Are Mjaavatten
Are Mjaavatten 2021년 7월 6일
편집: Are Mjaavatten 님. 2021년 7월 6일
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:
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
  댓글 수: 1
mehtap agirsoy
mehtap agirsoy 2021년 7월 6일
Thanks for the help. You're right my code store the whole data in a single array which I don't want. I've made some changes.Please correct me if I my idea is wrong, size of 3rd dimension changes with the 4rd dim so I need cell array.
M_Vel=cell(1,1,nsyll{k},k) creates what I want val(:,:,1) ={21600×4 double}
but when it starts to 4th dim doesn't store the data val(:,:,1,1) = {0×0 double}
don't know how to fix

댓글을 달려면 로그인하십시오.

Are Mjaavatten
Are Mjaavatten 2021년 7월 7일
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));
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)'...
Average over trials by taking the mean (over dimension 1) and remove the trials dimension by the squeeze command:
averages = squeeze(mean(results));
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.

mehtap agirsoy
mehtap agirsoy 2021년 7월 7일
Many thanks for the help. I've tried your suggestion but couldn't. I really got lost. I'm not good at matlab so have a real uphill struggle ahead of me now. I've attached some data if you're able to help, appreciate it. Many thanks again.
  댓글 수: 1
Are Mjaavatten
Are Mjaavatten 2021년 7월 8일
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.

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by