Matlab and preparing matrix lines

  • Context: MATLAB 
  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Lines Matlab Matrix
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 1K views
hokhani
Messages
606
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