Using vectorization to shorten run time

1 回表示 (過去 30 日間)
Emily Heil
Emily Heil 2021 年 4 月 22 日
コメント済み: Emily Heil 2021 年 4 月 22 日
Have never used vectorization (or much Matlab in general) & I'm attempting to use it to shorten the run time of my code.
I want to iterate through each value of "p" and "d" to create a 2x10,000 matrix of all possible combinations.
This is my current attempt at using vectorization.... p & d increase together so there's only 100 columns of X being filled. Is there a way to fix this while still minimizing run time? Thanks!
p = -200:4:200;
d = -10:.2:10;
X = zeros(2,10000);
for i = 1:1:100
X(1,i) = p(i);
X(2,i) = d(i);
end

採用された回答

James Tursa
James Tursa 2021 年 4 月 22 日
E.g.,
[P,D] = meshgrid(p,d); % generate all possible combinations
X = [P(:) D(:)]; % combine them into side-by-side column vectors
  1 件のコメント
Emily Heil
Emily Heil 2021 年 4 月 22 日
great, thank you!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by