Index in position 1 exceeds array bounds (must not exceed 33).
3 views (last 30 days)
Show older comments
m=44;
n=33;
x1=10;
x2=30;
y1=15;
y2=23;
p=2;
X=x1:((x2-x1)/(m-1)):x2;
Y=(y1:((y2-y1)/(n-1)):y2);
binX=X(1):((X(length(X))-X(1))/(m/p)):X(length(X));
binY=Y(1):((Y(length(Y))-Y(1))/(n/p)):Y(length(Y));
[Xmesh,Ymesh] = meshgrid(X,Y);
Z= (sin(Xmesh) + cos(Ymesh));
figure(1)
pcolor(Xmesh,Ymesh,Z)
title('Matrice iniziale')
shading flat
colorbar
colormap(jet)
for k=1:length(binX)-1
fx=find(X>binX(k) & X<=binX(k+1));
E=Z(fx,:)
% ERRORE sul 1 ciclo for
for l=1:length(binY)-1
fy=find(Y>binY(l) & Y<=binY(l+1));
EE=Z(:,fy)
MediaCell=mean(EE(:))
TOTCell(k,l)=MediaCell
end
end
XCell=(binX(1:end-1))
YCell=(binY(1:end-1))
figure(2)
pcolor(XCell,YCell,TOTCell')
title('Media Matrice')
shading flat
colorbar
colormap(jet)
I have a problem witch this script. I made a matrix Z (33X44) on X and Y grid.I made a second grid defined in (binX, binY) smaller than in X and Y should I adapt the array Z on new grid. I initially found the values for which X and beetween binX(k) and bin(k+1) and incorporated into a new array and values. I did the same for the grid Y just that witch the estracted values in the arrey EE i did then the mean. I plugged them into a final grid. How do i resolve this error so that I realize the arrey?
4 Comments
Luna
on 30 Nov 2018
What do you mean by second grid?
The problem is: your Z doesn't have more than 33 element in its rows.
But you are trying to get 34th, 35th, ... 44th element of your Z according to your fx.
Because your fx gets values from 1 to 44.
Answers (2)
Adam Danz
on 30 Nov 2018
Edited: Adam Danz
on 30 Nov 2018
Disclaimer: I don't know what your code is supposed to do and I didn't understand your discription. I've made two small changes that make your code work (ie, no errors) but it's up to you to make sure these changes make sense. The 4 comments in the code mark the 4 lines I changed. The footnotes contain additional comments.
m=44;
n=33;
x1=10;
x2=30;
y1=15;
y2=23;
p=2;
% X=x1:((x2-x1)/(m-1)):x2;
% Y=(y1:((y2-y1)/(n-1)):y2);
X = linspace(x1, x2, m); %Replaced 2 lines above with these 2 lines [1]
Y = linspace(y1, y2, n); %Replaced 2 lines above with these 2 lines
binX=X(1):((X(length(X))-X(1))/(m/p)):X(length(X));
binY=Y(1):((Y(length(Y))-Y(1))/(n/p)):Y(length(Y));
[Xmesh,Ymesh] = meshgrid(X,Y);
Z= (sin(Xmesh) + cos(Ymesh));
figure(1)
pcolor(Xmesh,Ymesh,Z)
title('Matrice iniziale')
shading flat
colorbar
colormap(jet)
for k=1:length(binX)-1
fx=find(X>binX(k) & X<=binX(k+1));
E=Z(:, fx) %Changed from E=Z(fx,:) [2]
% ERRORE sul 1 ciclo for
for l=1:length(binY)-1
fy=find(Y>binY(l) & Y<=binY(l+1));
EE=Z(fy, :) %Changed from EE=Z(:,fy) [2]
MediaCell=mean(EE(:))
TOTCell(k,l)=MediaCell
end
end
XCell=(binX(1:end-1))
YCell=(binY(1:end-1))
figure(2)
pcolor(XCell,YCell,TOTCell')
title('Media Matrice')
shading flat
colorbar
colormap(jet)
[1] very tiny difference in one of the elements of X between your and my version (-3.5527e-15 difference);
[2] this is a major change; confirm that it's reasonable.
2 Comments
Adam Danz
on 3 Dec 2018
In you k-loop, you are finding the index values in X range from binX(k) and binX(k+1) (not the X-values but the index values). That's what find() returns.
On loop #17 (k=17), fx=[33, 34] and you're trying to pull the 33rd and 34th row of Z but Z only has 33 rows. That's where your code breaks.
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!