Expanding a matrix with for loop
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Dev
2025 年 4 月 23 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!