Array indices must be positive integers or logical values

3 次查看(过去 30 天)
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 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by