I'm trying to see if I can improve the speed of a for loop and an if condition statement. Basically it does a lookup on non repeating key values into an array and gets the value from another column.
If I run 100000 values it takes about 13 seconds see code below. Is there a way to make this more efficient? Ps i'm using octave 3.8.1 which works with matlab
%test if lookup statment
clear all, clc, tic, clf;
num_to_test=100000 %amount of numbers to test
a1=(1:1:num_to_test)';
a2=(a1.*num_to_test);
array=[a1,a2]; %array where values are stored
lookupval=(randperm(num_to_test,num_to_test/2)/4)'; %lookup these random values of non repeating integers and floats and get another value
amp=[];
freq=[];
found_array=[];
notfound_array=[];
for ii=1:1:rows(lookupval)
if (find(lookupval(ii)==array(:,1))) %if you find a lookup value in array
%disp('found');
[row,col] = find(lookupval(ii) == array(:,1));
amp=[amp;array(row,2)];
freq=[freq;array(row,1)];
found_array=[freq,amp];
else %add lookup value to another array and make amp value zero
notfound_arraytmp=[lookupval(ii),0];
notfound_array=[notfound_array;notfound_arraytmp];
endif
end
comb_array=[found_array;notfound_array];
sort_comb_array=sortrows(comb_array,1); %sort array by first col incrementing
fprintf('\nfinally Done-elapsed time -%4.4fsec- or -%4.4fmins- or -%4.4fhours-\n',toc,toc/60,toc/3600);