Matlab and preparing matrix lines

  • Context: MATLAB 
  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Lines Matlab Matrix
Click For Summary
SUMMARY

The discussion focuses on generating a matrix in Matlab where each row corresponds to a specific value from the vector U, which contains the values [210; 100; 150]. The user seeks to avoid using a "for loop" for efficiency. A proposed solution involves creating intermediate matrices, specifically U2 = U * ones(1,100) and Y2 = ones(3,1) * ky, to ensure dimension compatibility. This method is deemed efficient for reasonably sized problems, although alternatives may exist for larger datasets.

PREREQUISITES
  • Understanding of Matlab matrix operations
  • Familiarity with the linspace function in Matlab
  • Knowledge of element-wise operations in Matlab
  • Basic concepts of matrix dimensions and broadcasting
NEXT STEPS
  • Explore Matlab's array manipulation functions
  • Learn about Matlab's broadcasting capabilities
  • Investigate alternative methods for matrix generation without loops in Matlab
  • Study performance optimization techniques in Matlab for large datasets
USEFUL FOR

Matlab users, data analysts, and engineers looking to optimize matrix operations and improve code efficiency without using loops.

hokhani
Messages
601
Reaction score
22
In the Matlab program below

U=[210; 100; 150]
ky= linspace(-.2,.2);
qx=sqrt(((150-U)/.2)^2-ky.^2)

I want to have qx as a matrix in which each row is corresponding to one value of U: the first row corresponds to 210, the second to 100 and ... How should I do this? (I want to avoid the "for loop" if possible).
 
Physics news on Phys.org
One way would be to make intermediate matrices so that the dimensions agree.
U2 = U * ones(1,100);
Y2 = ones(3,1)*ky;
Then your function will be referring to the same index in both reference matrix.
There might be a better way that is more effecient with your storage space, but for reasonably sized problems, this would do the trick.
 
  • Like
Likes   Reactions: hokhani

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K