How to fix: Index Exceeds Matrix Dimensions.
    7 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
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}
    alpha(n)=1/(n+1)^2;  
    z1{n}=w1{n}+alpha(n)*(w1{n}-w1{n-1});
    w1{n+1}=z1{n};
end
% 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};
0 Kommentare
Antworten (2)
  dpb
      
      
 am 16 Okt. 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 =
     1
>> numel(w1{1})
ans =
     3
>> 
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 =
        2647
>> w1{1}(n-1)
ans =
        8268
>> 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...
0 Kommentare
  Cris LaPierre
    
      
 am 16 Okt. 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.
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!