Error "Index in position 2 is invalid."

6 次查看(过去 30 天)
%Heres the code I am working with.
gmat=zeros(118,400);
fid1=fopen('tomo_data.txt','r');
gdata= fscanf(fid1,'%f %f %f',[3,1600]);
for i=1:1600
gmat(gdata(1,i),gdata(2,i)) = gdata(2,i);
end
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Heres the error:
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in ch5q2b (line 16)
gmat(gdata(1,i),gdata(2,i)) = gdata(2,i);
Attached to this question is the txt file used. Any help would be appriciated.
  2 个评论
Guilherme
Guilherme 2022-10-21
What's your final goal with yoour code? The problem you're facing is that your file has multiple floating points and this is probably what is causing the error of "index in position x is invalid".
The following code is, for instance invalid and has the exact same error as yours:
a = zeros(5,5);
a(1,0.5)
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Also, you're creating all your code (vector and for) with predetermined values. This may not be a good practice if your input text change in the future. Instead you could go with something like:
fid1=fopen('tomo_data.txt','r');
gdata= fscanf(fid1,'%f %f %f',[3,1600]);
gmat = zeros(size(gdata));
Therefore, it'd be better if we could try to really understand what you're trying to do :D
Jacob Allen
Jacob Allen 2022-10-21
% Formulating the model matrix (a column vector) with m rows
mmat=zeros(400,1); %initialize with zeros
for i=14:16
for j=12:13
k = 20*(i-1) + j; % convert row and column description into the
% column vector element
mmat(k)=-0.1;
end
end
% Multiplying G matrix with the M matrix to produce synthetic data
% This will be our data matrix d (another column vector)
dmat=gmat*mmat;
% use lsqr to do inversion: use the above synthetic data dmat to
% recover the model. Need G matrix again.
minvert = lsqr(gmat,dmat);
% Finally, plot the staring model and recovered (by inversion) model
% need to put one-column model vector into original 20 by 20 grid first.
mmatgrid = reshape(mmat,[20,20]);
minvertgrid = reshape(minvert,[20,20]);
subplot(1,2,1) % using subplot to plot multiple panels in one figure
imagesc(mmatgrid,[-0.1 0.05]); colormap(gray);
title('The starting model');
colorbar('SouthOutside');
axis equal
xlim([0,20]);
ylim([0, 20]);
subplot(1,2,2)
imagesc(minvertgrid,[-0.1 0.1]); colormap(gray);
title('The recovered model by inversion');
colorbar('SouthOutside');
axis equal
xlim([0,20]);
ylim([0, 20]);
Here is what I have below that code I provided previously. I tried to explain what I am doing in the green text.

请先登录,再进行评论。

回答(1 个)

Cris LaPierre
Cris LaPierre 2022-10-21
The error is because your second index, the column index for gmat, which is gdata(2,i), is 0. That is not a valid index. Indices in MATLAB must be positive integers (or logicals).
gmat = 1:3;
% works
gmat(1,1)
ans = 1
% your error
gmat(1,0)
Index in position 2 is invalid. Array indices must be positive integers or logical values.
  6 个评论
Jacob Allen
Jacob Allen 2022-10-23
This code was given to me as a shell code. Im only suppose to be able to change certain things to get a different outcome. I don't know what the original intent of each piece of this code means. I just need the error to be fixed so that I can output the graphs.
Cris LaPierre
Cris LaPierre 2022-10-24
The error has been explained, but we don't have enough detail to tell you what it should be doing instead.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by