필터 지우기
필터 지우기

'Array indices must be positive integers or logical values' error

조회 수: 1 (최근 30일)
Emily Gallagher
Emily Gallagher 2019년 9월 28일
답변: Raunak Gupta 2019년 10월 1일
I keep getting the 'Array indices must be positive integers or logical values' error specifically n=f(i).
f = (2000:100:3000);
l = length(f);
tt = zeros(1,1);
y = 4;
for i = i:l
n = f(i);
A = randn(n,n);
b = rand(n,1);
tic()
for j = 1:y %repeat nrep times for more precise timing
[L,U] = lu(A);
y = forwardsub(L,b);
x = backsub(U,y);
end
tt(i) = toc()/y %average recorded time
end

답변 (1개)

Raunak Gupta
Raunak Gupta 2019년 10월 1일
Hi,
The value of i is not initialized and you may start with some numerical value greater than zero at first. Otherwise n = f(i) return error because i is assigned as zero by default and in MATLAB, array indexing starts from 1. Even using logical value ‘true’ and ‘false’, the ‘false’ value will return an empty array instead of throwing an error. So you may try to initialize the looping variable.

카테고리

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