Is there a faster way to run my code?

Hello ,
i wrote the code bellow . Is there any faster and more efficient way to run this code (not using for-loop for example or something like that):
for count1=1:length(r)
for count2=1:length(C)
distance(count2,:)=abs(r(count1,:)-C(count2,:));
dist(count2)=sum(distance(count2,:),2);
end
[dist_hard index_hard(count1)]=min(dist);
end
The problem here is that when r or C contain many elements the code is slow and i its more than obvious that i dont want that .
Any help would be valuable .

 Accepted Answer

[dist_hard, index_hard] = min( pdist2(r, distance, 'cityblock'), [], 2);
Note: the distance measure you are using is the L1-norm, also known as "city block".

11 Comments

I am not sure how is that line you wrote making my code faster.
Also i am getting this message : Warning: Converting non-floating point data to double.
and the programm doesnt terminate .
what is class(r ) and class(distance) ?
pdist2() is the routine designed to take distances from each point in one set to each point in another set. The code I posted replaces your entire loop.
I am studying telecommunications right now , so think that r is the signal in the reciever and C is all the possible entries we have in the transmitter . My code runs perfectly but it is very slow .
For example if r is 10^3x7 matlab needs 30 seconds , and if i change r to 10^4x7 matlab needs 5 mins.
Thats why i am asking for a faster solution .
You did not answer the question about class() of those variables?
class(r)=logical
class(C)=double
[dist_hard, index_hard] = min( pdist2(double(r), C, 'cityblock'), [], 2);
By the way, in your code, if r is logical and C is double but only has values 0 and 1, then abs(r-C) is r~=C
Is C only approximately integer, such as if you are trying to reconstruct based on signal levels? If so then what range are the values? Is this reconstruction of a signal based upon constellation? Constellations are typically represented as complex.
I am trying to find the bit error rate for hamming encoding n=7 k=4 .
In the code i originally posted i am trying to find minimum distance from the signal r(10^3 x n=7).
C is a codebook that contains 2^k x n elements .So , in my case C is 16x7
Basically , in my code i keep the first row of the r and then compare it to each of the 16 Cs(and keep doing that until the last row of r).
Does C consist of only values 0 and 1? If so then the distance from r(count1,:) to C(count2,:) is
nnz(r(count1,:) ~= C(count2,:))
and you could vectorize over all C entries as
sum(r(count1,:) ~= C,2)
providing you are using R2016b or later.
If C does consist entirely of 0 and 1, then you can do your entire calculation as
[dist_hard, index_hard] = max(r*C.',[],2);
Note that in the case of ties in the distance, this code will pick the first of them.
Thank you so much for your effort !

Sign in to comment.

More Answers (2)

Bruno Luong
Bruno Luong on 23 Sep 2020
Edited: Bruno Luong on 23 Sep 2020
Use knnsearch if you have the right toolbox (I don't so the code is untested)
[index_hard, dist_hard] = knnsearch(C,r,'K',1,'Distance','cityblock')
Bruno Luong
Bruno Luong on 27 Sep 2020
Edited: Bruno Luong on 27 Sep 2020
For binary arrays
r=rand(50,8)>0.5;
C=rand(60,8)>0.5;
[dmin, index_hard] = min(sum(xor(r,permute(C,[3 2 1])),2),[],3);
index_hard'

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 23 Sep 2020

Commented:

on 29 Sep 2020

Community Treasure Hunt

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

Start Hunting!