有没有更好的方法来做这个for循环?
for i = find(A > 42)
B(A(i), i) = B(A(i), i) + 1;
end
A
是一个整数数组。B
是一个max(A)
×length(A)
矩阵。
例子:
A = reshape(magic(3), 1, 9); %# 8 3 4 1 5 9 6 7 2
B = zeros(max(A), length(A));
for i = find(A > 3)
B(A(i), i) = B(A(i), i) + 1;
end
B = [
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0
]