我有一个1s
和-1s
矩阵,随机点缀0s
:
%// create matrix of 1s and -1s
hypwayt = randn(10,5);
hypwayt(hypwayt > 0) = 1;
hypwayt(hypwayt < 0) = -1;
%// create numz random indices at which to insert 0s (pairs of indices may
%// repeat, so final number of inserted zeros may be < numz)
numz = 15;
a = 1;
b = 10;
r = round((b-a).*rand(numz,1) + a);
s = round((5-1).*rand(numz,1) + a);
for nx = 1:numz
hypwayt(r(nx),s(nx)) = 0
end
输入:
hypwayt =
-1 1 1 1 1
1 -1 1 1 1
1 -1 1 0 0
-1 1 0 -1 1
1 -1 0 0 0
-1 1 -1 -1 -1
1 1 0 1 -1
0 1 -1 1 -1
-1 0 1 1 0
1 -1 0 -1 -1
我想计算列中重复nonzero
个元素的次数,以生成这样的内容:
基本思想是(由@rayryeng提供)对于每一列,每次你点击一个唯一的数字,你开始递增一个累积的运行计数器,并且每次你达到与前一个相同的数字时它会递增。一旦你点击一个新号码,它就会被重置为1,除非是你点击0的情况,那么这就是0
预期输出:
hypwayt_runs =
1 1 1 1 1
1 1 2 2 2
2 2 3 0 0
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 2 0 1 2
0 3 1 2 3
1 0 1 3 0
1 1 0 1 1
最简洁的方法是什么?
答案 0 :(得分:3)
作为Dev-IL的动力,这是一个使用循环的解决方案。尽管代码是可读的,但我认为它很慢,因为你必须单独遍历每个元素。
hypwayt = [-1 1 1 1 1;
1 -1 1 1 1;
1 -1 1 0 0;
-1 1 0 -1 1;
1 -1 0 0 0;
-1 1 -1 -1 -1;
1 1 0 1 -1;
0 1 -1 1 -1;
-1 0 1 1 0;
1 -1 0 -1 -1];
%// Initialize output array
out = ones(size(hypwayt));
%// For each column
for idx = 1 : size(hypwayt, 2)
%// Previous value initialized as the first row
prev = hypwayt(1,idx);
%// For each row after this point...
for idx2 = 2 : size(hypwayt,1)
% // If the current value isn't equal to the previous value...
if hypwayt(idx2,idx) ~= prev
%// Set the new previous value
prev = hypwayt(idx2,idx);
%// Case for 0
if hypwayt(idx2,idx) == 0
out(idx2,idx) = 0;
end
%// Else, reset the value to 1
%// Already done by initialization
%// If equal, increment
%// Must also check for 0
else
if hypwayt(idx2,idx) ~= 0
out(idx2,idx) = out(idx2-1,idx) + 1;
else
out(idx2,idx) = 0;
end
end
end
end
>> out
out =
1 1 1 1 1
1 1 2 2 2
2 2 3 0 0
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 2 0 1 2
0 3 1 2 3
1 0 1 3 0
1 1 0 1 1
答案 1 :(得分:2)
我认为应该有更好的方法,但这应该有效
使用cumsum
,diff
,accumarray
&amp; bsxfun
%// doing the 'diff' along default dim to get the adjacent equality
out = [ones(1,size(A,2));diff(A)];
%// Putting all other elements other than zero to 1
out(find(out)) = 1;
%// getting all the indexes of 0 elements
ind = find(out == 0);
%// doing 'diff' on indices to find adjacent indices
out1 = [0;diff(ind)];
%// Putting all those elements which are 1 to zero and rest to 1
out1 = 0.*(out1 == 1) + out1 ~= 1;
%// counting each unique group's number of elements
out1 = accumarray(cumsum(out1),1);
%// Creating a mask for next operation
mask = bsxfun(@le, (1:max(out1)).',out1.');
%// Doing colon operation from 2 to maxsize
out1 = bsxfun(@times,mask,(2:size(mask,1)+1).'); %'
%// Assign the values from the out1 to corresponding indices of out
out(ind) = out1(mask);
%// finally replace all elements of A which were zero to zero
out(A==0) = 0
<强>结果:强>
<强>输入:强>
>> A
A =
-1 1 1 1 1
1 -1 1 1 1
1 -1 1 0 0
-1 1 0 -1 1
1 -1 0 0 0
-1 1 -1 -1 -1
1 1 0 1 -1
0 1 -1 1 -1
-1 0 1 1 0
1 -1 0 -1 -1
<强>输出:强>
>> out
out =
1 1 1 1 1
1 1 2 2 2
2 2 3 0 0
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 2 0 1 2
0 3 1 2 3
1 0 1 3 0
1 1 0 1 1
答案 2 :(得分:1)
在rayryeng's answer的基础上,以下是我对基于循环的解决方案的看法。
输入:
hypwayt = [
-1 1 1 1 1
1 -1 1 1 1
1 -1 1 0 0
-1 1 0 -1 1
1 -1 0 0 0
-1 1 -1 -1 -1
1 1 0 1 -1
0 1 -1 1 -1
-1 0 1 1 0
1 -1 0 -1 -1 ];
expected_out = [
1 1 1 1 1
1 1 2 2 2
2 2 3 0 0
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 2 0 1 2
0 3 1 2 3
1 0 1 3 0
1 1 0 1 1 ];
实际代码:
CNT_INIT = 2; %// a constant representing an initialized counter
out = hypwayt; %// "preallocation"
out(2:end,:) = diff(out); %// ...we'll deal with the top row later
hyp_nnz = hypwayt~=0; %// nonzero mask for later brevity
cnt = CNT_INIT; %// first initialization of the counter
for ind1 = 2:numel(out)
switch abs(out(ind1))
case 2 %// switch from -1 to 1 and vice versa:
out(ind1) = 1;
cnt = CNT_INIT;
case 0 %// means we have the same number again:
out(ind1) = cnt*hyp_nnz(ind1); %//put cnt unless we're zero
cnt = cnt+1;
case 1 %// means we transitioned to/from zero:
out(ind1) = hyp_nnz(ind1); %// was it a nonzero element?
cnt = CNT_INIT;
end
end
%// Finally, take care of the top row:
out(1,:) = hyp_nnz(1,:);
正确性测试:
assert(isequal(out,expected_out))
我想通过使用一些&#34;复杂的&#34;可以进一步简化它。 MATLAB函数,但恕我直言,它似乎足够优雅:)
注意:out
的顶行计算两次(一次在循环中,一次在结束时),因此计算值两次相关的效率很低。但是,它允许将整个逻辑放在numel()
上运行的单个循环中,在我看来,这证明了这一点额外的计算。
答案 3 :(得分:1)
这是一个很好的问题,因为@rayryeng没有提出一个矢量化解决方案,所以这里有几行 - 好吧,这不公平,我花了半天时间来完成这个。基本思想是使用cumsum
作为最终功能。
p = size(hypwayt,2); % keep nb of columns in mind
% H1 is the mask of consecutive identical values, but kept as an array of double (it will be incremented later)
H1 = [zeros(1,p);diff(hypwayt)==0];
% H2 is the mask of elements where a consecutive sequence of identical values ends. Note the first line of trues.
H2 = [true(1,p);diff(~H1)>0];
% 1st trick: compute the vectorized cumsum of H1
H3 = cumsum(H1(:));
% 2nd trick: take the diff of H3(H2).
% it results in a vector of the lengths of consecutive sequences of identical values, interleaved with some zeros.
% substract it to H1 at the same locations
H1(H2) = H1(H2)-[0;diff(H3(H2))];
% H1 is ready to be cumsummed! Add one to the array, all lengths are decreased by one.
Output = cumsum(H1)+1;
% last force input zeros to be zero
Output(hypwayt==0) = 0;
预期的产出:
Output =
1 1 1 1 1
1 1 2 2 2
2 2 3 0 0
1 1 0 1 1
1 1 0 0 0
1 1 1 1 1
1 2 0 1 2
0 3 1 2 3
1 0 1 3 0
1 1 0 1 1
让我补充一些解释。当然是第二个问题,我花了一段时间才弄清楚如何快速计算连续相同值的长度。第一个只是一个小技巧来计算整个事情没有任何for循环。如果你直接收集H1
,你会得到一些偏移的结果。通过采用某些键值的局部差异并在这些序列结束之后删除它们,以符合cumsum的方式去除这些偏移。这些特殊值是带编号的,我也是第一行(H2
的第一行):每个第一列元素看起来与前一列的最后一个元素不同。
我希望现在更清楚了(并且没有一些特殊案例的缺陷......)。