Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jul 8, 2008 #1

    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:


    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.

    Thanks in advance for your help!
  2. jcsd
  3. Jul 8, 2008 #2
    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 =


    >> x(1,:).*y

    ans =

    2 4
    Hope this helps you out :approve:
  4. Jul 8, 2008 #3
    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:


    and so on...

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


    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:


    For the second time step:


    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?
  5. Jul 8, 2008 #4
    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).
  6. Jul 8, 2008 #5
    Elaboration of structure would be helpful
  7. Jul 9, 2008 #6
    The AA.BB structure is called spacecraft.model in my own simulation. It will be a 1x1000 structure with the following fields:


    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)​

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook