How do i use for to populate matrix
이전 댓글 표시
iv'e run this code bust i get the error message below. Index exceeds matrix dimensions.
Error in Import_dat_files (line 20) x(j,:) = A((j-1)*nx+1:j*nx,1);
Can anyone help please this is the code below
filename= 'B00049.dat';
delimiterIn= ' ';
headerlinesIn = 3;
A = importdata(filename,delimiterIn,headerlinesIn);
%% Matrices%%%%
xi = 214; yj = 134;
x= zeros(yj,xi);
y= zeros(yj,xi);
u= zeros(yj,xi);
v= zeros(yj,xi);
%% Matrix population
for j=1:xi x(j,:) = A((j-1)*xi+1:j*xi,1);
y(j,:) = A((j-1)*xi+1:j*xi,2);
u(j,:) = A((j-1)*xi+1:j*xi,3);
v(j,:) = A((j-1)*xi+1:j*xi,4);
end
댓글 수: 1
Dennis
2018년 7월 31일
When j is 214 you try to access A((214-1)*214+1:214*214,2)), which is A(45583:45796,2). My guess is that A is smaller than that.
채택된 답변
추가 답변 (1개)
Ernest Adisi
2018년 7월 31일
0 개 추천
댓글 수: 2
Guillaume
2018년 7월 31일
Please don't use 'Answer this question' for comments.
I would still recommend you change your variable names to more meaningful names to avoid this sort of bugs
And certainly, replace your slooow for loop with the fast reshape I have given.
Ernest Adisi
2018년 7월 31일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!