Expanding a matrix with for loop

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.

回答 (1 件)

Dev
Dev 2025 年 4 月 23 日

0 投票

Hi Mareeah,
To expand the matrix ‘alfa’ from 1x20 to 40x20, we can first declare the expanded matrix. Next, we can use the linspacefunction 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.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2017b

質問済み:

2021 年 4 月 9 日

回答済み:

Dev
2025 年 4 月 23 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by