我有这些错误:下标必须是整数1(2 ^ 31)1或逻辑值

时间:2022-07-30 10:39:48

I'm starting to program and choose octave couse it's the free "version" of matlab, the program they use here at my university.
So, these problem apears when i try to run my code:

我开始编程,选择octave couse,这是matlab的免费版本,在我大学使用的程序。所以,当我试图运行我的代码时,这些问题会发生:

error: I(0): subscripts must be either integers 1 to (2^31)-1 or logicals

错误:我(0):下标必须是整数1(2 ^ 31)1或逻辑值

and I don't know why, because when I put R in place of RR, the code works.
I thought that maybe the problem was that R-Rn isn't reeeally equal 0, but something close to it, ans thus the problem, but after doing some tests with simple numbers, I found out that it was actually 0, but maybe it's diferent for matrixes.
Please help.
oh, and don't pay attention to the notes, i'm from brazil.

我不知道为什么,因为当我放R代替RR的时候,代码就起作用了。我想可能问题是R-Rn不等于0,但是接近它的东西,因此问题,但是在做了一些简单的测试之后,我发现它实际上是0,但它可能是矩阵的diferent。请帮助。哦,不要注意这些音符,我来自巴西。

clear          
tic                                       %inicio da cronometragem      
f = @(x) 16*x.^6 + 8*x -6;                %definir função      
df = @(x) 96*x.^5 + 8;                    %definir derivada da função  
tol = 10E-4;                              %define a tolerancia  
xmin = -5;                                %menor valor para X  
xmax = 5;                                 %maior valor para X  
ymin = -5;                                %menor valor para Y  
ymax = 5;                                 %maior valor para Y  
npx = 8;                                  %número de pontos do eixo X  
npy = 8;                                  %número de pontos do eixo Y
dx = (xmax-xmin)/npx;                     %calculo da distância entre os 
pontos do eixo X  
dy = (ymax-ymin)/npy;                     %calculo da distância entre os 
pontos do eixo Y  
A = zeros(npy+1, npx+1);                  %define matriz cheia de 0, do 
tamanho definido por npx e npy  
x = xmin:dx:xmax;                         %define tamanho e distancia entre os pontos de uma linha      
y= ymin:dy:ymax;                          %define tamanho e distancia entre os pontos de uma coluna      
A = x+i*y';                               %preenche a matriz A com os valores desta multiplicação  
[m n] = size(A);                          %define m e n como o tamanho da linha e coluna, respectivamente, da matriz A  
I = zeros(m,n);                           %cria outra matriz, I, de mesmo tamanho de A, mas cheia de 0  
i = 0;                                    %numero de interações definido como 0  
R = I;  
for kk = 1:50                         %define numero de interações do metodo de newton   
  An = A-f(A)./df(A);                 %método de newton  
  i = i + 1;                          %aumentar o numero de interação a cada vez que repete  
  Rn = R  
  R = abs((An - A)) < tol             %cria uma matriz R com 1 ou 0 se a posição alcançou a tolerancia ou não  
  RR = R - Rn  
  I(RR) = i                           %preenche a matriz I com o numero de interação de cada posição da matriz A  
  A = An;                             %iguala A com An para que o metodo de newton possa continuar  
  end  

imagesc(I)                                %forma o fractal  
toc                                       %para o cronômetro          

2 个解决方案

#1


2  

It works for R because R is a logical array - indexing with a logical array accepts 0 as a value because 0 in logical array indexing means 'don't apply to this element'.

它适用于R,因为R是一个逻辑数组——用一个逻辑数组进行索引,可以接受0作为一个值,因为逻辑数组索引中的0表示“不应用于这个元素”。

When you subtract your array Rn, RR becomes an array of doubles. Indexing this way cannot accept 0 as there is no 0th element.

当你减去数组Rn时,RR就变成了一组双打。索引这种方式不能接受0,因为没有第0元素。

You need to convert RR to a logical array (the function is 'logical' in MATLAB) for it to work.

您需要将RR转换为一个逻辑数组(在MATLAB中这个函数是“合乎逻辑的”)。

#2


1  

When I run the code I get that RR is all zeros. In MATLAB indexes start at 1 (so zero is an invalid index). Try changing RR = R - Rn to RR = R - Rn + 1. This at least lets the code run and produce an image (although I'm not sure if the image is correct).

当我运行代码时,我得到的RR都是零。在MATLAB中,索引从1开始(所以0是无效索引)。试着改变RR = R - Rn到RR - Rn + 1。这至少让代码运行并生成图像(尽管我不确定图像是否正确)。

#1


2  

It works for R because R is a logical array - indexing with a logical array accepts 0 as a value because 0 in logical array indexing means 'don't apply to this element'.

它适用于R,因为R是一个逻辑数组——用一个逻辑数组进行索引,可以接受0作为一个值,因为逻辑数组索引中的0表示“不应用于这个元素”。

When you subtract your array Rn, RR becomes an array of doubles. Indexing this way cannot accept 0 as there is no 0th element.

当你减去数组Rn时,RR就变成了一组双打。索引这种方式不能接受0,因为没有第0元素。

You need to convert RR to a logical array (the function is 'logical' in MATLAB) for it to work.

您需要将RR转换为一个逻辑数组(在MATLAB中这个函数是“合乎逻辑的”)。

#2


1  

When I run the code I get that RR is all zeros. In MATLAB indexes start at 1 (so zero is an invalid index). Try changing RR = R - Rn to RR = R - Rn + 1. This at least lets the code run and produce an image (although I'm not sure if the image is correct).

当我运行代码时,我得到的RR都是零。在MATLAB中,索引从1开始(所以0是无效索引)。试着改变RR = R - Rn到RR - Rn + 1。这至少让代码运行并生成图像(尽管我不确定图像是否正确)。