Matlab中Cell(单元数据)的用法
1. x = c(k)使用这种”()”形式的返回的是cell类
2. x = c{k}使用这种”{}”形式的返回的是cell中的内容
matlab中使用struct创建结构体
MATLAB中的结构数组
matlab 结构(struct)数组
图与网络教程及MATLAB算法实现
建模算法(五)——图与网络
1. edge = struct(‘to’,v,’next’,heads(u),’w’,w);
ctrl+c 或 ctrl+break
MATLAB:排列组合
1. nchoosek
Binomial coefficient or all combinations
- C = nchoosek(n,k)
函数描述: 从 n 个元素中 一次选 k 个元素的所有组合数 C(注意,C是一个数值)。
C = n!/((n–k)! k!);
- C = nchoosek(v,k)
函数描述: 从 向量 v 中 一次选其中 k 个元素 的所有组合 C (注意:C是一个矩阵,列数 为 k )
2. combntns
All possible combinations of set of values 从给定集合set中列出所有可能的subset个元素的组合
- combos = combntns(set,subset)
returns a matrix whose rows are the various combinations that can be taken of the elements of the vector set of length subset.
temp = [1,2,3,4,5;6,7,8,9,10;11,12,13,14,15];
C = [];
result = [];
for j=1:3
C = [nchoosek(temp(j,:),3)];
for j2=1:3
if j2==j
continue;
end
l1 = length(C);
l2 = length(temp(j2,:));
B = repmat(temp(j2,:)',1,l1);
B = reshape(B',1,l1*l2)';
C1 = repmat(C,l2,1);
result = [result;cat(2,C1,B)];
end
end
结果如下:
1 2 3 6
1 2 4 6
1 2 5 6
1 3 4 6
1 3 5 6
1 4 5 6
2 3 4 6
2 3 5 6
2 4 5 6
3 4 5 6
1 2 3 7
1 2 4 7
1 2 5 7
1 3 4 7
1 3 5 7
1 4 5 7
2 3 4 7
2 3 5 7
2 4 5 7
3 4 5 7
…
实际应用部分值有修改,特别注意l,比如我这里是前面三个,后面一个,在实际应用中为
l1 = size(C,1);
l2 = size(B,2);
A = [1,2,3;2,3,4;1,2,3;2,3,5]
[B, M, N] = unique(a,'rows')
result = sum(bsxfun(@eq,N(:),[1:length(M)]))
结果如下:
A =
1 2 3
2 3 4
1 2 3
2 3 5
B =
1 2 3
2 3 4
2 3 5
M =
1
2
4
N =
1
2
1
3
result =
2 1 1
%考虑可信度,即权重
A = [1,2,3;2,3,4;1,2,3;2,3,5];
H = [1;0.5;0.5;1];
[B, M, N] = unique(A,'rows');
T = bsxfun(@eq,N(:),[1:length(M)])
resultofa = sum(bsxfun(@times,T,H))
resultofn = sum(T)
T =
1 0 0
0 1 0
1 0 0
0 0 1
resultofa =
1.5000 0.5000 1.0000
resultofn =
2 1 1
a = [3,4,2,2;1,5,3,2];
b=[sort(a(:,1:3),2),a(:,4)];
b =
2 3 4 2
1 3 5 2
%Test (a,b,c,d,w1)-(c,d,a,b,w2)
A=[1,2,3,4,1;1,5,2,6,1.1;3,4,1,2,1.2;3,4,1,2,2];
A2=[[A(:,1:2),A(:,3:4),A(:,5)];[A(:,3:4),A(:,1:2),-A(:,5)]];
[B, M, N] = unique(A2(:,1:4),'rows');
T = bsxfun(@eq,N(:),[1:length(M)]);
result = [B,sum(bsxfun(@times,T,A2(:,5)))'];
result = result(find(result(:,5)>0),:)
result =
1.0000 5.0000 2.0000 6.0000 1.1000
3.0000 4.0000 1.0000 2.0000 2.2000
A1=[1,2,3,4,1;1,5,2,6,1;1,2,3,4,2;3,4,1,2,2;3,4,5,6,2];
A2=[1,2,3,4,1;1,5,2,6,1;1,2,3,4,2;3,4,5,6,2];
A11=repmat(A1,1,size(A2,1));
A11=reshape(A11',size(A1,2),size(A1,1)*size(A2,1))'; A22=repmat(A2,size(A1,1),1); result = bsxfun(@eq,A11(:,:),A22(:,:))';
resultsum = sum(result);
result1 = A11(find(resultsum(:)==5),:)
[B, M, N] = unique(result1(:,5),'rows');
result2 = sum(bsxfun(@eq,N(:),[1:length(M)]))
result1 =
1 2 3 4 1
1 5 2 6 1
1 2 3 4 2
3 4 5 6 2
result2 =
2 2