Expanding a matrix with for loop
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
A product from a for loop is a 1 x 20 matrix alfa and 1 x 20 matrix m.
I would like to expand the matrix alfa to 40 x 20 matrix by adding an equidistant increment with linspace to every value in the row.
I tried the following:
for j=1:numel(alfa)
b(j)= linspace(alfa(j),2*alfa(j),40)
r0(b)=m(b)./(cosd(alfa(b)/2));
end
but it's not working and I can't think of a solution.
Please let me know if you have a suggestion.
0 Commenti
Risposte (1)
Dev
il 23 Apr 2025
Hi Mareeah,
To expand the matrix ‘alfa’ from 1x20 to 40x20, we can first declare the expanded matrix. Next, we can use the “linspace” function to make sure each column is a linspace from alfa(j) to 2*alfa(j) (40 points, as referred in the code provided above). I have attached a reference code snippet below which achieves the same-
% Expand alfa
alfa_expanded = zeros(40, 20);
for j = 1:20
alfa_expanded(:,j) = linspace(alfa(j), 2*alfa(j), 40).';
end
For more information regarding the usage of the “linspace” function in MATLAB, please refer to the documentation link below-
Finally, since the code provided above calculates ‘r0’ for each value, we can do the same on the expanded matrix outside the ‘for’ loop, as shown below-
% Compute r0
r0 = m ./ cosd(alfa_expanded/2);
I hope the above approach helps resolves your query.
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!