Sorting and functional arithmetic of two vectors

1 回表示 (過去 30 日間)
Suhas Deshpande
Suhas Deshpande 2011 年 7 月 1 日
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

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2011 年 7 月 1 日
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))');
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2011 年 7 月 1 日
CORRECTED (07/01/2011 18:25 MSK)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by