0

Thanks goes out to Shai for getting my code to be much more efficient. The link to the original thread is here. Original Thread

How can I have a loop check and stop if a number in the "array_all" array has been repeated from the "x" array.

Example:

Here's the code below:

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

The output is:

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

We run into problems with 1.0000 0.5000 0.2500 we know it's due to the fact that it found the number 1.0000 in a previous check of the array array_all but how can we fix it?

The array we are trying to get is below:

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: The numbers will not be this simple there will be thousands of values. And I won't know when they will repeat.

Ps: I'm using octave 3.8.1

Community
  • 1
  • 1
Rick T
  • 3,349
  • 10
  • 54
  • 119
  • Hi Rick, I'm not sure about the status of this question. Is this a follow-up question that arose after Shai helped you figure out the previous one? If so, why does it read very similarly to the other one? Is it an improved formulation of the same question? Then please go back and edit your previous question, do not post a new one. Has Shai's answer on the previous question helped you? If so, then consider upvoting and/or accepting his answer. Please note that on SO there are no "threads", because this is not a message board. It is a Q&A site. – A. Donda Feb 02 '15 at 15:08
  • possible duplicate of [Numerical grouping using matlab / octave and not repeating values found in main array](http://stackoverflow.com/questions/28263795/numerical-grouping-using-matlab-octave-and-not-repeating-values-found-in-main) – A. Donda Feb 02 '15 at 15:09
  • @A. Donda yes a problem popped up with the code he helped me with he made it much easier but a problem popped up, hence I referenced the question which shows his last comment about another type of loop may be needed. Which he mentioned in one of his comments. – Rick T Feb 02 '15 at 15:15
  • Still, is this a new question or a new version of the old question? If the latter, please go back and edit the old question – you can copy-paste the text from here. And then you can delete this one. – A. Donda Feb 02 '15 at 15:17
  • @A. Donda this was his comment so it's a new issue "RickT it's a nasty thing about the last line: 1 appears before in the line [4 2 1] but this line is also removed. I'm afraid loop is the only way here,,, – Shai " – Rick T Feb 02 '15 at 15:18
  • Ok, fair enough. Still, consider upvoting Shai's answer on the other question, and maybe mention this new version. – A. Donda Feb 02 '15 at 15:19
  • I mean of course: upvote if it helped to answer your question... – A. Donda Feb 02 '15 at 15:21

1 Answers1

1

Using a loop

%// create the data
x=[9,8,7,6,5,4,3,2,1]
array_all = bsxfun( @times, x(:), [1 .5 .25] );

Start pruning

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

Now leave only valid entries

array_all = array_all(valid, : );

You can try this out at ideone.

Shai
  • 111,146
  • 38
  • 238
  • 371