Index in position 3 exceeds array bounds

조회 수: 7 (최근 30일)
Pralayakaveri Yogendra
Pralayakaveri Yogendra 2023년 12월 4일
댓글: Image Analyst 2023년 12월 4일
following is the code where the error is occuring:
function sim_data = frac_and_disp(sim_data)
% Calculate cartesian coordinates and the displacement of the atoms...
disp('Transforming cartesian to fractional coordinates and calculating displacement')
sim_data.frac_pos = zeros(3, sim_data.nr_atoms, sim_data.nr_steps);
sim_data.displacement = zeros(sim_data.nr_atoms, sim_data.nr_steps);
d = zeros(3,1);
for atom = 1:sim_data.nr_atoms
uc = [0 0 0]; % Keep track of the amount of unit cells the atom has moved
%For the first time step:
sim_data.frac_pos(:,atom,1) = sim_data.cart_pos(:,atom,1)/sim_data.lattice;
start = sim_data.cart_pos(:,atom,1); % Starting position
for time = 2:sim_data.nr_steps
sim_data.frac_pos(:,atom,time) = sim_data.cart_pos(:,atom,time)/sim_data.lattice;
for i = 1:3
frac_diff = sim_data.frac_pos(i,atom,time)-sim_data.frac_pos(i,atom,time-1);
% If frac_pos differs by more than 0.5 from the previous time step the atom changed unit cell
if frac_diff > 0.5
uc(i) = uc(i)-1;
elseif frac_diff < -0.5
uc(i) = uc(i)+1;
end
end
% Calculate displacement:
for i = 1:3
d(i) = ( sim_data.cart_pos(i,atom,time) - start(i) + uc*sim_data.lattice(:,i) )^2;
end
sim_data.displacement(atom,time) = sqrt(sum(d));
end
end
end
  댓글 수: 4
Pralayakaveri Yogendra
Pralayakaveri Yogendra 2023년 12월 4일
data is being taken from OUTCAR file of VASP
Dyuman Joshi
Dyuman Joshi 2023년 12월 4일
I'm not familiar with VASP.
Please provide a link to access the data or upload it here.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2023년 12월 4일
Evidently sim_data.cart_pos does not have 3 dimensions. What does this show if you put it just before that line that throws the error?
size(sim_data.cart_pos)
What size is reported to the command window?
Please see this so you can do what all the rest of us do:
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 12월 4일
"Evidently sim_data.cart_pos does not have 3 dimensions."
The 3rd dimension should be a singleton one but it does exist.
And as OP is trying to acces the 1st page of the 3rd dimension, it should work, even if the 3rd dimension is not defined explicitly.
y = magic(5)
y = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
y(1,2,1)
ans = 24
z = rand(1,10)
z = 1×10
0.5882 0.3849 0.4663 0.3267 0.1466 0.7569 0.0844 0.6456 0.3476 0.0765
z(1,3,1)
ans = 0.4663
Image Analyst
Image Analyst 2023년 12월 4일
The index in position 3 of
sim_data.frac_pos(:,atom,1) = sim_data.cart_pos(:,atom,1)/sim_data.lattice;
is 1 and it's a constant so I don't know what index they're talking about. But I see another potential problem. What is
size(sim_data.lattice)
? If that is a 2-D array, and the user is thinking that sim_data.cart_pos(:,atom,1) is another 2-D array, that might be a problem. Might try squeeze, and maybe ./ should be used instead of / alone unless you were hoping for some kind of automatic expansion.
tempArray = squeeze(sim_data.cart_pos(:,atom,1)) ./ sim_data.lattice;
size(tempArray)
sim_data.frac_pos(:,atom,1) = tempArray;
If that doesn't work, maybe try a for loop to assign one element at a time.

댓글을 달려면 로그인하십시오.

카테고리

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