Numerical grouping using matlab / octave and not repeating values found in main array

懵懂的女人 提交于 2019-12-06 21:44:34
Shai

You can try

array_all = bsxfun( @times, x(:), [1 .5 .25] ); %// generate for all values

Now, using ismember we prune the rows of array_all

[Lia Locb] = ismember( x(:), reshape( array_all.', [], 1 ) ); %// order elements of array_all row by row

By construction all elements of x are in array_all but we want to prune rows for which the first element already appears in previous rows.

firstRowToAppearIn = ceil(Locb/3); 

So

toBePruned = 1:numel(x) > firstRowToAppearIn; %// prune elements that appear in array_all in a row preceding their location in x

array_all(toBePruned,:) = []; %// remove those lines

Now we can define array_dyn_name-s according to array_all.
Using eval is HORRIBLE, instead we use structure with dynamic field names:

st = struct();
for ii=1:size(array_all,1)
    nm = sprintf('array_dyn_name%d',ii);
    st.(nm) = array_all(ii,:);
end

It seems like ismember's second output (Locb) might be ordered differently between Matlab and octave (and maybe between newer and older versions of matlab). So, here's an alternative, using :

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 ); 

PS,
Apart from using eval, there is another cavity in your implementation: you do not pre-allocate space for array_all - this may cause your code to run significantly slower than it actually needs to be. See, for example this thread about pre-allocation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!