# MatLab: How to add a matrix to a structure without a loop?

## Main Question or Discussion Point

Hi,

I have a structure AA.BB, which goes from AA.BB(1) to AA.BB(1000). I have for example a matrix "X" of size [1000x3], which I want to add to this structure, such that it becomes like this:

AA.BB(1).X(1)​
AA.BB(2).X(1)​
...​
AA.BB(1000).X(1)​

I want the same for X(2) and X(3).

Can this be done without using a loop, in which all entries of the matrix are individually placed in the structure? I dont want to use a loop at all.

I know it may be easier to just keep the matrix "X", but I want to know if such a thing can be done.

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
I have a structure AA.BB, which goes from AA.BB(1) to AA.BB(1000).
Do you mean you have a vector of length 1000?
To multiply individual elements of a vector with a matrix you use the command '.*'
note the dot, this is important.
I have done a wee demonstration to help you with your problem. Just adapt it to move into 3 column matrix regime. I have just made a small matrix 'x' and a small vector 'y'. Shown below is how to multiply a row or column of x by y:

----------------------------
>> y=[2 2]

y =

2 2

>> x=[1 2;3 4]

x =

1 2
3 4

>> x(1,:)

ans =

1 2

>> x(:,1)

ans =

1
3

>> x(1,:).*y

ans =

2 4
----------------------------
Hope this helps you out I know the command .*, but don't think it is really what I need. I need to copy the matrix I have into a structure, but it gives errors, which I will post later in this message.

I have this structure AA.BB. This BB has to go from 1 to 1000, which are indications for time steps in a simulation. For each time step, there are many characteristics that are in the structure.

As an example, for the first time step the structure looks like this:

AA.BB(1).cc​
AA.BB(1).dd​
AA.BB(1).ee​
AA.BB(1).ff​
AA.BB(1).gg​

and so on...

For the second time step, these characteristics (cc, dd, ee, ff, gg) have some different values:

AA.BB(2).cc​
AA.BB(2).dd​
AA.BB(2).ee​
AA.BB(2).ff​
AA.BB(2).gg​

Now I have a matrix "X", which has 1000 rows (one row for each separate time step) and 3 columns (representing the x-, y- and z-coordinate of some parameter).

This matrix "X" has to be copied somehow into the structure without a loop. What I want to get, for example for the first time step:

AA.BB(1).X(1)​
AA.BB(1).X(2)​
AA.BB(1).X(3)​

For the second time step:

AA.BB(2).X(1)​
AA.BB(2).X(2)​
AA.BB(2).X(3)​

What I have tried is the following commands:

AA.BB(: ).X(1) = X(:,1);
AA.BB(: ).X(2) = X(:,2);
AA.BB(: ).X(3) = X(:,3);

This gives the following error message:

??? Scalar index required for this type of multi-level indexing.

Can it be done another way, without a loop?

Wouldn't any matrix procedure involving multiple elements at the computational level involve a loop of some sort, due to the nature of having more than one element?

Either way, you may wish to read up on MathWorks' built in documentation on the "struct" command. If I recall, it should allow you to pre-allocate the structure layout and the structural elements through a series of matrices and cell arrays/non cell inputs (easy to convert).

I need to copy the matrix I have into a structure,
Elaboration of structure would be helpful

The AA.BB structure is called spacecraft.model in my own simulation. It will be a 1x1000 structure with the following fields:

time
meanJulianDate
position
velocity​

I have the following matrices:

t [1000x1]
MJD [1000x1]
r [1000x3]
V [1000x3]​

Using a loop I can easily fill the structure:

for i = 1:1000

spacecraft.model(i).time = t(i,1)
spacecraft.model(i).meanJulianDate = MJD(i,1)
spacecraft.model(i).position = r(i,1:3)
spacecraft.model(i).velocity = V(i,1:3)​
end​

Question is, how to do the same thing without the use of a loop? I'm just wondering if this is possible and if so, how.