跨行计算孤岛中出现的次数 - MATLAB
Count occurrences of ones in islands across rows - MATLAB
我需要以下函数的帮助:for 循环或向量化代码中的 histc 和 numel。我有一个可以是任何维度的矩阵。代码需要输出一个元素在一个区间内出现的次数,直到每一行的末尾。因此对于下面的示例,我想找出第 1 行中数字 1 出现的次数。因此在第 1 行中,数字 1 在被两个 0 打断之前出现了两次。然后它再次出现在最后一列中。所以输出将是 2 1。
感谢您的帮助。谢谢
x = hist( data, numel(unique(data)) );
y = histc( data, unique(data) );
data (input) 5x5
1 1 0 0 1
1 1 1 1
0 0 1
0 0 1 1 1
1 0 1 1 1
y (output)
2 1
2 2
1
3
1 3
假设 x
是输入数组,这可能是一种方法 -
[nrows,ncols] = size(x);
new_ncols = ncols + 2;
%// Pad one column of zeros on left and right sides of x
x_pz = [zeros(nrows,1) x zeros(nrows,1)]
%// Flatten padded x
x_pzf = reshape(x_pz',[],1)'
%// Start & end indices of islands of ones for flattened padded x
starts = strfind(x_pzf,[1 0]);
ends = strfind(x_pzf,[0 1])
row_ids = ceil(starts/new_ncols); %// row IDs for each island of ones
%// Start & end indices of islands of ones for flattened non-padded (corrected) x
starts_cor = ends - 2*(row_ids-1)
ends_cor = starts - (2*row_ids-1)
%// Get number of elements in each island of ones
counts = ends_cor - starts_cor + 1
%// Bin row_ids for each row of input array
counts_per_row = histc(row_ids,1:nrows)
%// Now setup output array with conts for each island corresponding to each
%// row ending up in its each row and setting the blank spaces as NaNs
mask = bsxfun(@ge,counts_per_row,(1:max(counts_per_row))') %//'
y = NaN(size(mask))
y(mask) = counts
y = y'
代码运行-
>> x (modified from the original one to test out more varied situations)
x =
1 1 0 0 1 1
1 1 0 1 1 0
0 0 0 0 1 0
0 0 1 1 1 0
1 0 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0
0 1 0 1 0 1
>> y
y =
2 2 NaN
2 2 NaN
1 NaN NaN
3 NaN NaN
1 4 NaN
6 NaN NaN
NaN NaN NaN
1 1 1
如果您正在寻找一种更直观、更简洁的方式来获取和显示最终输出,您可以使用元胞数组。所以,你可以这样做 -
>> ycell = arrayfun(@(n) counts(row_ids==n),1:nrows,'Uni',0);
>> celldisp(ycell)
ycell{1} =
2 2
ycell{2} =
2 2
ycell{3} =
1
ycell{4} =
3
ycell{5} =
1 4
ycell{6} =
6
ycell{7} =
[]
ycell{8} =
1 1 1
我需要以下函数的帮助:for 循环或向量化代码中的 histc 和 numel。我有一个可以是任何维度的矩阵。代码需要输出一个元素在一个区间内出现的次数,直到每一行的末尾。因此对于下面的示例,我想找出第 1 行中数字 1 出现的次数。因此在第 1 行中,数字 1 在被两个 0 打断之前出现了两次。然后它再次出现在最后一列中。所以输出将是 2 1。
感谢您的帮助。谢谢
x = hist( data, numel(unique(data)) );
y = histc( data, unique(data) );
data (input) 5x5
1 1 0 0 1
1 1 1 1
0 0 1
0 0 1 1 1
1 0 1 1 1
y (output)
2 1
2 2
1
3
1 3
假设 x
是输入数组,这可能是一种方法 -
[nrows,ncols] = size(x);
new_ncols = ncols + 2;
%// Pad one column of zeros on left and right sides of x
x_pz = [zeros(nrows,1) x zeros(nrows,1)]
%// Flatten padded x
x_pzf = reshape(x_pz',[],1)'
%// Start & end indices of islands of ones for flattened padded x
starts = strfind(x_pzf,[1 0]);
ends = strfind(x_pzf,[0 1])
row_ids = ceil(starts/new_ncols); %// row IDs for each island of ones
%// Start & end indices of islands of ones for flattened non-padded (corrected) x
starts_cor = ends - 2*(row_ids-1)
ends_cor = starts - (2*row_ids-1)
%// Get number of elements in each island of ones
counts = ends_cor - starts_cor + 1
%// Bin row_ids for each row of input array
counts_per_row = histc(row_ids,1:nrows)
%// Now setup output array with conts for each island corresponding to each
%// row ending up in its each row and setting the blank spaces as NaNs
mask = bsxfun(@ge,counts_per_row,(1:max(counts_per_row))') %//'
y = NaN(size(mask))
y(mask) = counts
y = y'
代码运行-
>> x (modified from the original one to test out more varied situations)
x =
1 1 0 0 1 1
1 1 0 1 1 0
0 0 0 0 1 0
0 0 1 1 1 0
1 0 1 1 1 1
1 1 1 1 1 1
0 0 0 0 0 0
0 1 0 1 0 1
>> y
y =
2 2 NaN
2 2 NaN
1 NaN NaN
3 NaN NaN
1 4 NaN
6 NaN NaN
NaN NaN NaN
1 1 1
如果您正在寻找一种更直观、更简洁的方式来获取和显示最终输出,您可以使用元胞数组。所以,你可以这样做 -
>> ycell = arrayfun(@(n) counts(row_ids==n),1:nrows,'Uni',0);
>> celldisp(ycell)
ycell{1} =
2 2
ycell{2} =
2 2
ycell{3} =
1
ycell{4} =
3
ycell{5} =
1 4
ycell{6} =
6
ycell{7} =
[]
ycell{8} =
1 1 1