I have just started exploring world of vectorization. I got the 1-D vectorization down but i am having trouble vectorizing the following code.
CityPairs = [7 33 13 11 77 13 45 14 6];Offices = [1;3;7];nOffices = size(Offices,1);connection = zeros(nOffices);for i = 1:nOffices for j = 1:nOffices connection(i,j) = sum(Offices(i) == CityPairs(:,1)... & CityPairs(:,2) == Offices(j)); end connection(i,:) = connection(i,:);end disp(connection)
In this example there are 7 cities, three of which have offices. I want a pairwise matrix for the cities with offices to capture the sum of all the one way connections between each. The answer for above problem should be:
0 0 1 2 0 0 1 1 0
Any suggestions are welcome. Thanks in advance.
Keith
Best Answer