how to velocize it (vectorizing)

1 回表示 (過去 30 日間)
aldo
aldo 2023 年 8 月 20 日
編集済み: Bruno Luong 2023 年 8 月 20 日
a=magic(10)
b=[4;5;9;2;3;4;2;7;4;9] %width(a) element
x=ones(size(a));
for i=1:numel(b)
x(1:max(b(i)-1,1),i)=0
end

回答 (1 件)

Bruno Luong
Bruno Luong 2023 年 8 月 20 日
編集済み: Bruno Luong 2023 年 8 月 20 日
I don't know why a is matter beside that the first dimension is 10
a=magic(10);
b=[4;5;9;2;3;4;2;7;4;9]; %width(a) element
h = size(a,1);
x = double(ndgrid(1:h,b)>=b(:)')
x = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
;
  9 件のコメント
aldo
aldo 2023 年 8 月 20 日
編集済み: aldo 2023 年 8 月 20 日
"Yes for-loop is faster on onlive server (on my compter I get the opposite)." i don't use online server but my pc "Processore AMD Ryzen 9 5950X 16-Core Processor, 3401 Mhz, 16 core, 32 processori logici"
I think is better to use loop in my pc because it work with parallel processor
I've noticed dramatic improvements when it comes to vectorizing code where there is a loop inside another
thanks for taking the time
Bruno Luong
Bruno Luong 2023 年 8 月 20 日
編集済み: Bruno Luong 2023 年 8 月 20 日
"I've noticed dramatic improvements when it comes to vectorizing code where there is a loop inside another"
Not really. The speed depens what you do in the body of the loop(s), not loop are nested or not.
I know what I'm talking on speeding MATLAB code.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by