作业帮 > 综合 > 作业

matlab中的计数问题

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/28 14:39:59
matlab中的计数问题
0\x050\x054\x054\x054\x054\x050\x050\x054\x054\x054\x054 1;
4\x054\x050\x050\x050\x050\x050\x050\x050\x054\x050\x050 1;
4\x054\x050\x050\x050\x050\x050\x050\x050\x054\x050\x050 1;
4\x054\x050\x054\x054\x054\x054\x054\x050\x054\x050\x050 2;
4\x054\x050\x050\x050\x050\x054\x050\x050\x054\x050\x054 2;
4\x050\x050\x054\x054\x054\x054\x054\x050\x054\x050\x050 2;
4\x050\x050\x050\x050\x050\x054\x050\x050\x054\x050\x050 3 ;
4\x050\x050\x050\x054\x054\x054\x054\x054\x054\x050\x050 3;
4\x050\x050\x050\x054\x054\x054\x054\x054\x054\x050\x050 3;
若月份换到竖行,怎么计数每个月中成交量不是0的个数
我写了一个,
[h,w] = size(K);
P = zeros(max(K(:,w)),w-1);
for month = 1:max(K(:,w))
B = repmat(K(:,w) == month,w,1);
C = (K.*B > 0)';
D = sum(C)';
P(month,:) = D(1:w-1);
end
这个好像不行,不知道错在哪里,请指教
抱歉,周末出去玩了没看到.
之前的程序在
C = (A.*B > 0)';
D = sum(C)';
两行之所以要加'是因为matlab中的sum函数默认对列求和,所以先把矩阵转置,求和后再转置回来,相当于对行求了和;而这个问题中本身就是对列求和,所以不用转置操作了:
C = (K.*B > 0);
D = sum(C);
还有就是repmat函数原先是将一行做复制操作变成一个矩阵,写成:
B = repmat(A(h,:) == month,h,1);
而现在是将一列做复制操作,应该写成:
B = repmat(K(:,w) == month,1,w);
就可以了,程序:
clear;
K = [0\x050\x054\x054\x054\x054\x050\x050\x054\x054\x054\x054 1;
4\x054\x050\x050\x050\x050\x050\x050\x050\x054\x050\x050 1;
4\x054\x050\x050\x050\x050\x050\x050\x050\x054\x050\x050 1;
4\x054\x050\x054\x054\x054\x054\x054\x050\x054\x050\x050 2;
4\x054\x050\x050\x050\x050\x054\x050\x050\x054\x050\x054 2;
4\x050\x050\x054\x054\x054\x054\x054\x050\x054\x050\x050 2;
4\x050\x050\x050\x050\x050\x054\x050\x050\x054\x050\x050 3;
4\x050\x050\x050\x054\x054\x054\x054\x054\x054\x050\x050 3;
4\x050\x050\x050\x054\x054\x054\x054\x054\x054\x050\x050 3];
[h,w] = size(K);
P = zeros(max(K(:,w)),w-1);
for month = 1:max(K(:,w))
B = repmat(K(:,w) == month,1,w);
C = (K.*B > 0);
D = sum(C);
P(month,:) = D(1:w-1);
end
P
结果:
P =
2 2 1 1 1 1 0 0 1 3 1 1
3 2 0 2 2 2 3 2 0 3 0 1
3 0 0 0 2 2 3 2 2 3 0 0
再问: 1 2 3 4 5 6 7 8 9 910 11 12;
如果月份在最后一行,(如以上所示)以每一列为单位,找出每一列中成交量不为0的个数应该怎么做。
如第一列为8,第二列为4
再答: 这样就好了:sum(K(1:end-1, :) ~= 0)
结果:
ans =

8 4 1 3 5 5 6 4 3 9 1 2