如何在Matlab中矩阵的每对行上应用函数? [英] How to apply a function on every pair of rows of a Matrix in Matlab?
本文介绍了如何在Matlab中矩阵的每对行上应用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Matlab的新手,正在研究矩阵,我有点困惑.
I'm new to matlab and working with matrix and I'm kinda confused.
我应该制作一个名为M
的m
x n
矩阵,它的元素是-1
,1
和0
.
I'm supposed to make a m
x n
matrix called M
and it's elements are -1
, 1
and 0
.
我需要编写一个名为d(x,y)
的函数,如果x = -1
和y = 1
,该函数将返回1
.并返回0
否则.
I need to write a function called d(x,y)
which returns 1
if x = -1
and y = 1
. And returns 0
otherwise.
和另一个计算每列中d(m(i,j),m(k,j))之和的函数:
and another function which calculates the sum of d(m(i,j),m(k,j)) in every column:
请阅读注释中的示例.
如何找到总和?
我知道基本编程,但是我不知道该怎么做.
I know basic programming but I don't know how to do this.
推荐答案
您可以使用nchoosek
进行选择:
comb = nchoosek(1:size(m,1), 2);
result = zeros(1, length(comb)); % allocate the memory
% you can run some techniques to run a function on each row of comb
% which is mentinoned in other posts instead of the following code
for i = 1:length(comb)
result(i) = sum(abs(m(comb(i,1), :) - m(comb(i,2), :)) == 2);
end
这篇关于如何在Matlab中矩阵的每对行上应用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文