我是matlab的新手,正在使用矩阵,我有点困惑。
我应该做一个m
x n
矩阵,叫做M
,它的元素是-1
,1
和0
。
我需要编写一个名为d(x,y)
的函数,如果为x = -1
和y = 1
,则返回1
。否则返回0
。
另一个函数计算每列中d(m(i,j),m(k,j))的和:
请阅读评论中的示例。
如何求和?
我知道基本的编程,但我不知道怎么做。
发布于 2018-07-29 19:35:15
您可以使用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
https://stackoverflow.com/questions/51579179
复制相似问题