如何不使用 matlab / octave 重复数组中的值
How not to repeat values in array using matlab / octave
感谢 Shai 让我的代码更加高效。原始线程的 link 在这里。
Original Thread
如果 "array_all" 数组中的数字已从 "x" 数组重复,我如何进行循环检查并停止。
示例:
下面是代码:
x=[9,8,7,6,5,4,3,2,1]
array_all = bsxfun( @times, x(:), [1 .5 .25] ) %// generate for all values
eq_ = bsxfun( @eq, array_all, permute( x(:), [3 2 1] ) );
eq_ = max( eq_, [], 2 ); %// we do not care at which column of array_all x appeared
[mx firstRowToAppearIn] = max( squeeze(eq_), [], 1 );
toBePruned = 1:numel(x) > firstRowToAppearIn; %// prune elements that appear in array_all in a row preceding their location in x
pruned_array=array_all;
pruned_array(toBePruned,:) = []; %// remove those lines
st = struct();
for ii=1:size(pruned_array,1)
nm = sprintf('array_dyn_name%d',ii);
st.(nm) =pruned_array(ii,:);
end
pruned_array
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);
输出为:
array_all =
9.00000 4.50000 2.25000
8.00000 4.00000 2.00000
7.00000 3.50000 1.75000
6.00000 3.00000 1.50000
5.00000 2.50000 1.25000
4.00000 2.00000 1.00000
3.00000 1.50000 0.75000
2.00000 1.00000 0.50000
1.00000 0.50000 0.25000
pruned_array =
9.0000 4.5000 2.2500
8.0000 4.0000 2.0000
7.0000 3.5000 1.7500
6.0000 3.0000 1.5000
5.0000 2.5000 1.2500
我们 运行 遇到了 1.0000 0.5000 0.2500 的问题,我们知道这是因为它在之前的数组检查中找到了数字 1.0000 array_all 但我们该如何解决呢?
我们要获取的数组如下:
pruned_array =
9.0000 4.5000 2.2500
8.0000 4.0000 2.0000
7.0000 3.5000 1.7500
6.0000 3.0000 1.5000
5.0000 2.5000 1.2500
1.0000 0.5000 0.2500
PS:数字不会这么简单会有几千个值。而且我不知道他们什么时候会重复。
Ps: 我使用的是 Octave 3.8.1
使用循环
%// create the data
x=[9,8,7,6,5,4,3,2,1]
array_all = bsxfun( @times, x(:), [1 .5 .25] );
开始修剪
n = numel(x);
valid = false(n,1); %// at first, only first line is valid
valid(1) = true;
for ii=2:n, %// first line is valid by default
valid(ii) = ~any( reshape( array_all( valid, : ),[],1) == x(ii) );
end
现在只留下有效条目
array_all = array_all(valid, : );
您可以在 ideone 试试这个。
感谢 Shai 让我的代码更加高效。原始线程的 link 在这里。 Original Thread
如果 "array_all" 数组中的数字已从 "x" 数组重复,我如何进行循环检查并停止。
示例:
下面是代码:
x=[9,8,7,6,5,4,3,2,1]
array_all = bsxfun( @times, x(:), [1 .5 .25] ) %// generate for all values
eq_ = bsxfun( @eq, array_all, permute( x(:), [3 2 1] ) );
eq_ = max( eq_, [], 2 ); %// we do not care at which column of array_all x appeared
[mx firstRowToAppearIn] = max( squeeze(eq_), [], 1 );
toBePruned = 1:numel(x) > firstRowToAppearIn; %// prune elements that appear in array_all in a row preceding their location in x
pruned_array=array_all;
pruned_array(toBePruned,:) = []; %// remove those lines
st = struct();
for ii=1:size(pruned_array,1)
nm = sprintf('array_dyn_name%d',ii);
st.(nm) =pruned_array(ii,:);
end
pruned_array
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);
输出为:
array_all =
9.00000 4.50000 2.25000
8.00000 4.00000 2.00000
7.00000 3.50000 1.75000
6.00000 3.00000 1.50000
5.00000 2.50000 1.25000
4.00000 2.00000 1.00000
3.00000 1.50000 0.75000
2.00000 1.00000 0.50000
1.00000 0.50000 0.25000
pruned_array =
9.0000 4.5000 2.2500
8.0000 4.0000 2.0000
7.0000 3.5000 1.7500
6.0000 3.0000 1.5000
5.0000 2.5000 1.2500
我们 运行 遇到了 1.0000 0.5000 0.2500 的问题,我们知道这是因为它在之前的数组检查中找到了数字 1.0000 array_all 但我们该如何解决呢?
我们要获取的数组如下:
pruned_array =
9.0000 4.5000 2.2500
8.0000 4.0000 2.0000
7.0000 3.5000 1.7500
6.0000 3.0000 1.5000
5.0000 2.5000 1.2500
1.0000 0.5000 0.2500
PS:数字不会这么简单会有几千个值。而且我不知道他们什么时候会重复。
Ps: 我使用的是 Octave 3.8.1
使用循环
%// create the data
x=[9,8,7,6,5,4,3,2,1]
array_all = bsxfun( @times, x(:), [1 .5 .25] );
开始修剪
n = numel(x);
valid = false(n,1); %// at first, only first line is valid
valid(1) = true;
for ii=2:n, %// first line is valid by default
valid(ii) = ~any( reshape( array_all( valid, : ),[],1) == x(ii) );
end
现在只留下有效条目
array_all = array_all(valid, : );
您可以在 ideone 试试这个。