Clear Filters
Clear Filters

Sorting and functional arithmetic of two vectors

1 view (last 30 days)
I have a set of data say
x = [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9]
y = rand(20,1)
In x there are 9 couplets such that sum of their modulus is 10
x(1)+ x(11) = x(2)+ x(10)= x(3)+ x(9)=. . . = x(5)+ x(7) = 10
and
x(12)+ x(20) = x(13)+ x(20) = . . . = x(15)+ x(17) = -10
based on this property i want to make subsets of y
ie [y(1),y(11)], [y(2),y(10],... [y(12),y(20)]...[y(15),y(17)]
Someway I want the output to be
z = [f(y(1),y(11)) f(y(2),y(10)) . . . f(y(12),y(20)) . . . f(y(15),y(17))]
where f is a mathematical function say f(a,b) = sqrt(a^2+b^2)
Any suggestions ?
note: x(6)+x(6) = 10 and x(16)x(16) = -10 In that context f(y(6),y(6)) and f(y(16),y(26)) are not essential but its fine if algorithm includes the two

Answers (1)

Andrei Bobrov
Andrei Bobrov on 1 Jul 2011
lin = 1:length(x);
idx=sortrows([repmat(lin',1,2);nchoosek(lin,2)],1);
ouridx = idx(abs(sum(x(idx),2))==10,:);
y = rand(20,1);
z = ...
arrayfun(@(i1)sqrt(y(ouridx(i1,1))^2+y(ouridx(i1,2))^2),(1:size(ouridx,1))');

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!