Add operation is too slow

조회 수: 8 (최근 30일)
Igor Arkhandeev
Igor Arkhandeev 2021년 5월 27일
편집: Jan 2021년 5월 27일
Hi! I have some operation of summing two arrays (vd, bd) of different lengths ((vl + 1)^2 and ((bd + 1)^2). I can't do summing as d = vd + bd (error: different lengths). How can I do this operations faster? I compile MEX, but this did not give the required increase in speed. I need speed up adding because it using around 20 million
function [l, d] = add(vl, vd, bl, bd, lmax)
l = max([vl, bl, lmax]);
d = zeros((l + 1)^2, 1);
i = 1:(vl + 1)^2;
j = 1:(bl + 1)^2;
d(i) = d(i) + vd(i);
d(j) = d(j) + bd(j);
end
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 5월 27일
Can you share the detail example? What would be the result in the following case?
vd=[1 2 3]
bd=[4 5 6 7]
Igor Arkhandeev
Igor Arkhandeev 2021년 5월 27일
Of course!
d = [5 7 9 7]; % 1 + 4, 2 + 5, 3 + 6, 7

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 5월 27일
편집: Jan 2021년 5월 27일
It would save time to work with the real sizes instead of providing vl, if you actually need (vl+1)^2. This would save 60 mllion squaring operations.
function [l, d] = add(vl, vd, bl, bd, lmax)
l = max(max(vl, bl), lmax);
d = zeros((l + 1)^2, 1);
iv = (vl + 1)^2;
id = (bl + 1)^2;
d(1:iv) = d(1:iv) + vd(1:iv); % Or: d(1:iv) + vd; ?
d(1:id) = d(1:id) + bd(1:id); % Or: d(1:id) + bd; ?
end
The JIT acceleration profits from using x(a:b) instead of creating the index vector v=a:b; x(v).
If the inputs are uniquely explained, it would be possible to modify the function without too much guessing. Maybe this would be better already, maybe not:
function [len, d] = add(a, b, len)
na = numel(a);
nb = numel(b);
len = max(max(na, nb), len);
d = zeros(len, 1);
if na == nb
d = a + b;
elseif na > nb
d(nb+1:na) = a(nb+1:na);
d(1:nb) = a(1:nb) + b;
else
d(na+1:nb) = b(na+1:nb);
d(1:na) = a + b(1:na);
end
end

추가 답변 (1개)

Matt J
Matt J 2021년 5월 27일
편집: Matt J 2021년 5월 27일
vd=[1 2 3];
bd=[4 5 6 7];
N=max(numel(vd), numel(bd));
vd(end+1:N)=0;
bd(end+1:N)=0;
d=vd+bd
d = 1×4
5 7 9 7

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by