Idan Banani
Idan Banani

Reputation: 141

Matlab-Subscript indices must either be real positive integers or logicals

Where did I go wrong? the problem is in: R = sqrt(bsxfun(@minus,XX,(XX)').^2+bsxfun(@minus,YY,(YY)').^2);

EO = 8.8541e-12; %eps0
A2 = 1.0e-2; %2a
N = 100; %num of subareas in a plate
M = sqrt(N); %num of subareas in one axis
DX = A2/M; % 2b
DY = DX; %2b
DL = DX; %2b

%  SECOND, CALCULATE THE ELEMENS OF THE COEFFICIENT MATRIX A
% Write all the subareas centers coordinates to X and Y 
%  SECOND, CALCULATE THE ELEMENS OF THE COEFFICIENT MATRIX A
% Write all the subareas centers coordinates to X and Y 
x = linspace(DL*0.5,DL*(M-0.5),M);
y = x;
[XX,YY]=meshgrid(x,y);
%%L = NaN(N,N);
**R = sqrt(bsxfun(@minus,XX,(XX)').^2+bsxfun(@minus,YY,(YY)').^2);**
idx_diagL = find(eye(N)~=0);
idx_not_diagL = find(eye(N)==0);
L(idx_not_diagL) = DL^2./(4.*pi*EO*R(mod(idx_not_diagL,10),floor(idx_not_diagL/N)));
L(idx_diagL)  = DL*0.8814/(pi*EO); %ln(1+sqrt(2)= 0.8814

Upvotes: 1

Views: 3034

Answers (1)

Eitan T
Eitan T

Reputation: 32930

The problem is not where you say it is, but in the following line:

L(idx_not_diagL) = DL^2./(4.*pi*EO*R(mod(idx_not_diagL,10),floor(idx_not_diagL/N)));

In short, your problem is that you specify zero indices for matrix R, but in MATLAB zero indices are illegal (they start from 1, not 0!).

Now, where do you get zero indices? You index into R(..., ...) by using the following row and column subscripts:

mod(idx_not_diagL, 10)

and

floor(idx_not_diagL / N))

which both get zero values occasionally.

As a fix, I recommend using the following for the row indices:

mod(idx_not_diagL, 10) + 1

As for the column indices, there is another problem. The floor(...) expression ranges from 0 to 99. Once you fix it (I'm not sure what you're trying to achieve), it will work.

Upvotes: 4

Related Questions