Index in position 1 exceeds array bounds (must not exceed 33).

1 回表示 (過去 30 日間)
alessio grechi
alessio grechi 2018 年 11 月 30 日
コメント済み: Adam Danz 2018 年 12 月 3 日
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 件のコメント
Luna
Luna 2018 年 11 月 30 日
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.
alessio grechi
alessio grechi 2018 年 12 月 3 日
For second grid mean values of binX and binY.
Is there a way to get them without having to reverse the whole thing? that is, without having to use this expression?
> E = Z (:, fx);

サインインしてコメントする。

回答 (2 件)

Adam Danz
Adam Danz 2018 年 11 月 30 日
編集済み: Adam Danz 2018 年 11 月 30 日
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 件のコメント
alessio grechi
alessio grechi 2018 年 11 月 30 日
編集済み: alessio grechi 2018 年 11 月 30 日
I'm sorry but whar you did is not what I want.
1) find the values in X range from binX(k) and binX(k+1);
2) find the values in Y between binY(k) and binY(k+1);
3) for each cell that has been built, then the average of the cell along the Y while maintaining cost the X
4) construct the final array size equal to binX and binY.
I hope you understand my problem.
Adam Danz
Adam Danz 2018 年 12 月 3 日
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.

サインインしてコメントする。


Luna
Luna 2018 年 11 月 30 日
Try this,
E=Z(:,fx);

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by