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

Click For Summary

Discussion Overview

The discussion revolves around the challenge of adding a matrix to a structure in MatLab without using loops. Participants explore methods to incorporate a 1000x3 matrix into a structure that already contains multiple fields for each time step in a simulation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes the structure AA.BB and expresses the desire to add a matrix "X" such that each element of "X" corresponds to a specific field in the structure for each time step.
  • Another participant suggests using the element-wise multiplication command '.*' but acknowledges that it may not address the original poster's needs.
  • A participant elaborates on the structure's purpose in a simulation, detailing the fields and the matrix dimensions, and reiterates the need to copy the matrix into the structure without loops.
  • One participant questions whether any matrix operation involving multiple elements could inherently involve loops at a computational level.
  • A later reply suggests consulting MathWorks' documentation on the "struct" command for potential pre-allocation methods that could facilitate the desired structure layout.
  • Another participant seeks clarification on the structure's elaboration and how it relates to the matrix integration issue.
  • One participant provides an example of how they successfully fill the structure using a loop and asks if there is a way to achieve the same without looping.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a method to add the matrix to the structure without using loops. Multiple viewpoints and approaches are presented, but the discussion remains unresolved regarding a definitive solution.

Contextual Notes

Some participants mention potential errors encountered when attempting to use multi-level indexing with the structure. There are also references to the need for pre-allocation and the complexity of handling multiple elements in matrix operations.

Bruce-Almight
Messages
4
Reaction score
0
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 don't 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!
 
Physics 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 :approve:
 
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.
 

Similar threads

Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K