Info

This question is closed. Reopen it to edit or answer.

How and when does MatLab support symatrical vector operations?

1 view (last 30 days)
Marc Laub
Marc Laub on 25 Apr 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello,
my question is following?
I want to do a vector operation and I want to be sure that MatLab takes advantage of symmetrie.
Actually I want to get the nearest neighbour of points, so i have to calculate the distance from every point to each oter point. Since I am only interest about the order and not the distance, I neglect the sqrt since its only time consuming.
So my code loooks like following:
A=rand(10000,2);
Dista=(A(:,1)-A(:,1)').^2+(A(:,2)-A(:,2)').^2
The operation
A(:,1)-A(:,1)').^2
and
(A(:,2)-A(:,2)').^2
are symmetric, since (Ai-Aj)^2==(Aj-Ai)^2.
ans i want to take advantage of this.
I read about the fact that Matlab uses a differen BLAS when detecting symmetric operations, as you can read here:
So i did the same with my code:
A = rand(10000);
B = rand(10000);
timeit(@()(A-B').^2)
timeit(@()(A-A').^2)
It shows that the difference is nearly 0, maybe 0.001%, sometimes also the asymetric one is the "faster" one.
So is there a possibility to get the advantage of the symmetrie? Or do i have to malually do it and try to linearize the problem and do some logic indexing and hopefully save some time? I mean its clearly a logical operation and MatLab supports them, but how can i benefit from it in my case??
Many thanks in advance,
Best regards
Marc

Answers (0)

Community Treasure Hunt

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

Start Hunting!