Array indices must be positive integers or logical values

조회 수: 3 (최근 30일)
Aneesa Shahbaz
Aneesa Shahbaz 2021년 4월 12일
댓글: Aneesa Shahbaz 2021년 4월 14일
U0 = 0.1;
V0 = 0.5;
W = 1;
t = 1;
x = linspace(0,3); % create linear spacing in x-direction
y = linspace(0,3); % create linear spacing in y-direction
XX = zeros(length(x),length(y));
YY = zeros(length(x),length(y));
%looping in i and j-directions
for i = 1:length(x)
for j = 1:length(y)
%create x & y space
XX(i,j) = x(i);
YY(i,j) = y(j);
psi(i,j) = ((U0*V0)./W)*cos(W(t-YY(i,j)./V0))-V0*XX(i,j);
I get an error message of
Array indices must be positive integers or logical values.
Error in psi(i,j) = ((U0*V0)./W)*cos(W(t-YY(i,j)./V0))-V0*XX(i,j);

채택된 답변

Daniel Pollard
Daniel Pollard 2021년 4월 12일
You wrote
psi(i,j) = ((U0*V0)./W)*cos(W(t-YY(i,j)./V0))-V0*XX(i,j);
Try
psi(i,j) = ((U0*V0)./W)*cos(W*(t-YY(i,j)./V0))-V0*XX(i,j);
W is not a vector or matrix, so calling W(t-YY(i,j)./V0) won't return anything sensible. I think you mean to multiply W by the bracketed term in the cos.
Side note: i and j make terrible variable names in Matlab. They already have a built in value of the complex unit, so redefining that is likely to throw errors further down the line. I tend to use ii, jj or k for loop indices to avoid this problem.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by