close
Blogtrottr
Yahoo!奇摩知識+ - 分類問答 - 電腦網路 - 發問中
Yahoo!奇摩知識+ - 分類問答 - 電腦網路 - 發問中 
Join Delanceyplace

Sign up to receive a free quote from a non fiction book every weekday.
From our sponsors
matlab vectorize 變慢?
Mar 23rd 2014, 14:00

小弟想寫convolution的matlab程式
x1信號長度999,x2信號長度500。
聽說在matlab中,用向量代替迴圈可以增加速度
但是我做出來卻比較慢QQ
不知為何?

迴圈版本:
function y = myconv(x1, x2)
y=zeros(1,1499);
for n=2:1:501
    for k=1:1:n-1
        y(n)=y(n)+g(k)*h(n-k);
    end
end
for n=502:1:1000
    k=n-500;
    count=0;
    while  count<500
        y(n)=y(n)+g(k)*h(n-k);
        k=k+1;
        count=count+1;
    end
end
for n=1001:1:1499
    k=n-500;
    while  k<=999
        y(n)=y(n)+g(k)*h(n-k);
        k=k+1;
    end
end

向量化版本:(內層迴圈向量化)
function y = myconv(x1, x2)
y=zeros(1,1499);
for n=2:1499
    if(n<502)
        k=1:n-1;
        y(n)=sum(x1(k).*x2(n-k));
    elseif(n<1001)
         k=n-500:n-1;
         y(n)=sum(x1(k).*x2(n-k));
    else
        k=n-500:999;
        y(n)=sum(x1(k).*x2(n-k));
    end
end

This entry passed through the Full-Text RSS service — if this is your content and you're reading it on someone else's site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.

You are receiving this email because you subscribed to this feed at blogtrottr.com.

If you no longer wish to receive these emails, you can unsubscribe from this feed, or manage all your subscriptions
arrow
arrow
    全站熱搜
    創作者介紹

    winstonje 發表在 痞客邦 留言(0) 人氣()