Sorting and functional arithmetic of two vectors

2 Ansichten (letzte 30 Tage)
Suhas Deshpande
Suhas Deshpande am 1 Jul. 2011
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

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 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))');

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by