From: Anna Lappala on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hmtvc4$m7h$1(a)fred.mathworks.com>...
> Dear Anna!
>
> > I have a problem associated with the size of my matrix, which needs to be in 3D for 12000 given coordinates.. when i try to run my code, I get an error saying "Maximum variable size allowed by the program is exceeded." What can I do?
>
> A [12000 x 3] array is no problem in Matlab!
> Please post the line, which causes the error.
>
> BTW: Your array has 2 dimensions and is called a 2D array or matrix. It just contains 3D coordinates.
>
> Kind regards, Jan
Dear Jan,

Thank you for your correction -- I am a student and new to MatLab and programming, and I appreciate your comments! Here is the problem:

In order to build the matrix 'A', i `discretise the 3D space. Assuming that the coordinates are ranging from 0 to 12000 units, by storing them into 3 vectors xi,yi,zi where i is the atom index I do the following:

c = zeros(12000,12000,12000);   % initialisation with zeros
for i=1:length(X)
   c(round(X(i)), round(Y(i)), round(Z(i))) = 1;
end

is that correct?
Thank you for your attention.
Anna
From: Jan Simon on
"Dear Anna!

> In order to build the matrix 'A', i `discretise the 3D space. Assuming that the coordinates are ranging from 0 to 12000 units, by storing them into 3 vectors xi,yi,zi where i is the atom index I do the following:
>
> c = zeros(12000,12000,12000);   % initialisation with zeros
> for i=1:length(X)
>    c(round(X(i)), round(Y(i)), round(Z(i))) = 1;
> end

Not correct. Better:
c = zeros(12000, 3);
Then you fill the different rows by:
for i=1:length(X)
c(i, :) = [X(i), Y(i), Z(i)];
end

This can be done without a loop also:
c = [X, Y, Z];
assumed that X, Y, Z are [12000 x 1] vectors.

I'm really confused about what you want to achieve by:
c(round(X(i)), round(Y(i)), round(Z(i))) = 1
Perhaps you have to explain this and I miss the point.

A final thought: If your coordiantes are ranging from 0 to 12000, you have 12001 coordinates.

Kind regards, Jan
From: Walter Roberson on
Anna Lappala wrote:
>Here is the problem:
>
> In order to build the matrix 'A', i `discretise the 3D space. Assuming
> that the coordinates are ranging from 0 to 12000 units, by storing them
> into 3 vectors xi,yi,zi where i is the atom index I do the following:

> c = zeros(12000,12000,12000); % initialisation with zeros
> for i=1:length(X)
> c(round(X(i)), round(Y(i)), round(Z(i))) = 1;
> end

> is that correct?

Whether it is "correct" depends on exactly what you are trying to do. If
there is a good reason for doing things that way, then you can use
sparse matrices of type logical (sparse matrices support only logical
and double precision.) If you do use sparse matrices, I understand that
there is a single call that would build the matrix for you without the
need for the 'for' loop.