How do i use for to populate matrix

2 views (last 30 days)
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 Comment
Dennis
Dennis on 31 Jul 2018
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.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 31 Jul 2018
The text of the error does not match the code you're showing. Your code has the line
x(j,:) = A((j-1)*xi+1:j*xi,1);
The error says the line is
x(j,:) = A((j-1)*nx+1:j*nx,1);
One has nx, the other xi.
In any case, if you get the error, it's because A does not have xi^2 or nx^2 rows. What is
size(A)
  2 Comments
Ernest Adisi
Ernest Adisi on 31 Jul 2018
sorry, i changed the notation after running it. This is what it looks like originally
Guillaume
Guillaume on 31 Jul 2018
Edited: Guillaume on 31 Jul 2018
With an xi of 214, your text file needs to have at least 45796 rows of data for your A(j*xi, 1) (when j = xi) to be valid. Your attached file only has 28676 rows of data, short by about 17000 rows! The maximum xi you could have with that file is 169.
Don't know what you want to do about that.
_edit: just noticed that your
for j = 1:xi
does not make much sense. Your j index is the row and you've declared your destination arrays to have yj rows, not xi rows. So your loop should probably be:
for j = 1:yj
which would match the size of your attached file. Using meaningful variable names, preferably full words would have made the error obvious:
numcolumns = 214;
numrows = 134;
x = zeros(numrows, numcolumns); %better name than x?
y = zeros(numrows, numcolumns); %better name than y?
u = zeros(numrows, numcolumns); %better name than u?
v = zeros(numrows, numcolumns); %better name than v?
for row = 1:numrows
x(row, :) = A((row-1)*numcolumns+1:row*numcolumns, 1);
%...
end
But, the whole thing can be performed easily without a loop:
numcolumns = 214;
numrows = 134;
x = reshape(A(:, 1), numcolumns, numrows).'; %has to be reshape in a numcolumns x numrows matrix, then transposed into a numrows x numcols to get the same result as the loop
y = reshape(A(:, 2), numcolumns, numrows).';
%...

Sign in to comment.

More Answers (1)

Ernest Adisi
Ernest Adisi on 31 Jul 2018
hey, thanks resolved it before. works fine now
  2 Comments
Guillaume
Guillaume on 31 Jul 2018
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
Ernest Adisi on 31 Jul 2018
yeah, worked fine thanks. Can I ask you how I would go about doing the same thing for multiple files in a folder. I been having trouble trying to set a loop and file path to read read multiple files and process them accordingly. Whats the best way to do so?

Sign in to comment.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!