How to fix: Index Exceeds Matrix Dimensions.

11 views (last 30 days)
Dinoma Degefa
Dinoma Degefa on 16 Oct 2021
Answered: Cris LaPierre on 16 Oct 2021
Sometime it works and sometimes it says ''Index Exceeds Matrix Dimensions.'' What is wrong?
for n=2:1000;
w1{1}=fix(randi([-10000,10000],3,1)); %randemly generated vector in R^{3}
w1{2}=fix(randi([-10000,10000],3,1)); %randemly generated vector in R^{3}
% Error using -
% Matrix dimensions must agree.
% Error in kkkkkkmmmmmtebekin (line 5)
% z1{n}=w1{n}+alpha(n)*(w1{n}-w1{n-1});
%%% AND sometimes: sometimes it runs well.
% Index exceeds matrix dimensions.
% Error in kkkkkkmmmmmtebekin (line 6)
% w1{n+1}=z1{n};

Answers (2)

dpb on 16 Oct 2021
>> w1{1}=fix(randi([-10000,10000],3,1));
>> whos w1
Name Size Bytes Class Attributes
w1 1x1 128 cell
>> n=2;
>> w1{n}
Index exceeds the number of array elements (1).
The variable w1 is a cell array of size 1. While the content of the cell array is an array, the cell itself is only one element so addressing it outside a subscript of one is out of bounds.
>> numel(w1)
ans =
>> numel(w1{1})
ans =
also shows that the content of the cell array is only three elements so even addressing an element of the array above 3 will produce a similar problem.
>> w1{1}(n)
ans =
>> w1{1}(n-1)
ans =
>> n=n+2;
>> w1{1}(n)
Index exceeds the number of array elements (3).
so you see that when the loop index goes past 3, trying to address the 3-vector inside the cell array also fails.
Clearly you are trying to create some sort of sizable randomized variable, but it isn't clear at all from the above just what it is you're attempting.
Describe the problem you're trying to solve with, perhaps a short example of what an expected result would be for a small problem size (like upper limit on n being 10, say).
One small point -- NB: that fix() in the above is of no use since you're using randi() which generates only integers to begin with, and that the "3,1" size arguments generate a 3-vector of results, not something in a 3D array as your comment might tend to indicate you're thinking it does.
It just isn't clear what the intent of the above is, sorry...

Cris LaPierre
Cris LaPierre on 16 Oct 2021
I placed your code inside an infinite while loop and ran it for 15 minutes and no errors occurred. Perhaps there is some additional code you have not shared?
Try clearing your workspace and then running your code again.

Community Treasure Hunt

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

Start Hunting!