HEEELP : Index in position 2 is invalid. Array indices must be positive integers or logical values. !!!! why???

1 回表示 (過去 30 日間)
ezgi ünlü
ezgi ünlü 2020 年 1 月 17 日
回答済み: Steven Lord 2020 年 1 月 17 日
Hello, this is my code as you can see;
while i wrote the FOR part, matlab gives me this error. PLEASE HELP ME
opticaxis=[];
center=(length(y))/2;
for i=1:length(lambda)
opticaxis(i,:)=E_z(:,center,i)'/(max(E_z(:,center,i)));
end
mesh(x/(1e-6),lambda/(1e-6),opticaxis)
view(2)
colormap jet
ax = gca;
ax.YDir = 'normal'
title('GRIN with Plane Wave')
xlabel('x(um)')
ylabel('\lambda(um)')

回答 (2 件)

Steven Lord
Steven Lord 2020 年 1 月 17 日
center=(length(y))/2;
for i=1:length(lambda)
opticaxis(i,:)=E_z(:,center,i)'/(max(E_z(:,center,i)));
end
If y is a vector with an odd number of elements (as an example let's say y has 3 elements) you're asking for an element of E_z that doesn't exist (in the 3 element example, you're asking for elements in the 1.5th column of E_z.)
If y is not a vector, you probably shouldn't be using length. Use size and ask for the size of y in a specified dimension.

AdamG2013468
AdamG2013468 2020 年 1 月 17 日
Your for loop index is:
for i = 0:length(lambda)
Within your for loop you are attempting to create a new variable "opticaxis". You cannot define the opticaxis(0,:) point during the first index of your for loop. Try replacing your starting index value with a 1 insted of 0 (example below). This may not solve your problem just yet, but should get you closer. Also, what is the value of length(lambda)?
for i = 1:length(lambda)
  1 件のコメント
ezgi ünlü
ezgi ünlü 2020 年 1 月 17 日
編集済み: ezgi ünlü 2020 年 1 月 17 日
hello, thanks for your answer, i already write for i = 1:length(lambda) but yes, it is not solve my problem as you said. lambda is variant number, not constant about between 2.5 and 5. lambda is defined in the file.

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by