vectorising while loop insertion sort matlab -


array = [2 1 3 2 1]  = 2:length(array)     value = array(i);     j = - 1;      array_j=array(1:j);     array_j_indices=cumsum(array_j>value);     [~,n]=find(array_j_indices==1);     newarray=array;     array(n+1:i)=array_j(array_j>value);     j=j-max(array_j_indices);     array(j+1) = value;  end %forloop  disp(array); 

hello, saw code vectorising while loop insertion code cannot seem understand how works. how cumsum(array_j>value) work? understand , tested cumsum functions can't seem understand how rational operator of (array_j>value) works in within cumsum function under loop.

also, dont understand how [~,n]=find(array_j_indices==1) stores value matrix of n. store in columns because there not (~) in rows?

cumsum(array_j>value)?

array_j>value: due sorted nature of array_j, result zeros followed ones, e.g. [0 0 0 0 1 1 1 1]

cumsum(array_j>value) = [0 0 0 0 1 2 3 4]: @ 1 element equal 1.

[~,n]=find(array_j_indices==1); ?

because there 1 row, equal n=find(array_j_indices==1);.

fastest implementation?

note 'vectorised' code slower following (easier) implementation:

for = 2:length(array)     value = array(i);     j = - 1;      n=find(array(1:j)>value,1);      array(n+1:i)=array(n:j);     array(n) = value; end 

and slower built-in matlab sort method.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -