How can I apply a function to each column pair of a matrix?

Hi, I have a function that uses two vectors as inputs and gives an output as scalar. I want to apply this function to each pair of the matrix columns. For example, think about I have a matrix "x" with 5 columns. I want to apply my function to pairs of x(:,1)-x(:,2) + x(:,1)-x(:,3) + x(:,1)-x(:,4) + x(:,1)-x(:,5) + x(:,2)-x(:,3) + x(:,2)-x(:,4) + x(:,2)-x(:,5) + x(:,3)-x(:,4) + x(:,3)-x(:,5) + x(:,4)-x(:,5) +
And also I want store the resulting scalars in a symmetric 5x5 matrix with a diagonal of ones like correlation matrix.
Thanks,

 채택된 답변

Chad Greene
Chad Greene 2015년 10월 27일

0 개 추천

You can use nchoosek to get the permutations, then loop to call your function for each permutation.

댓글 수: 1

For example,
n = nchoosek(1:5,2)
gives you all the unique pairs from 1 to 5. To complete the list for your project you'd need to follow that with
n = [n;fliplr(n)];

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2015년 10월 27일

댓글:

2015년 10월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by