Seek: more efficient implementation

Hi,
I have a function H2_GFL (code below) which correctly performs the task I want it to do.
- (create a 3-dimensional cell; I will fill the the first dim with vectors later, at runtime; 2nd dim will contain random numbers bewtween 0 and 1.).
Is there any other code implementation by which this functionality can be done more efficiently? In my application I shall be calling (invoking) this function millions of times with N ranging from 20 to 300 and max_periods ranging from 1000 to 10,000. I use Matlab 7.01.24104 (R14, SP1) and R2015b (64-bit).
Regards
Sasanka
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function H_L1 = H2_GFL(N, max_periods)
num_rows = max_periods * N;
H_L1 = cell(num_rows, 2, N);
rand_supply = rand(num_rows, N);
for k = 1:1:N
for j = 1:1:num_rows
H_L1(j,2,k) = {rand_supply(j,k)};
H_L1(j,1,k) = {-98}; %% Dummy value, indicates end of data row
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Results with N = 3, max_periods = 5;
H_L1(:,:,1) =
[-98] [0.2715]
[-98] [0.4673]
[-98] [0.1713]
[-98] [0.7865]
[-98] [0.8774]
[-98] [0.0234]
[-98] [0.6326]
[-98] [0.5651]
[-98] [0.5763]
[-98] [0.8755]
[-98] [0.4644]
[-98] [0.7747]
[-98] [0.5460]
[-98] [0.7966]
[-98] [0.1721]
H_L1(:,:,2) =
[-98] [0.1791]
[-98] [0.5115]
[-98] [0.6513]
[-98] [0.4854]
[-98] [0.2452]
[-98] [0.5454]
[-98] [0.3246]
[-98] [0.9038]
[-98] [0.1873]
[-98] [0.5310]
[-98] [0.2923]
[-98] [0.9244]
[-98] [0.8981]
[-98] [0.1892]
[-98] [0.7519]
H_L1(:,:,3) =
[-98] [0.8209]
[-98] [0.9932]
[-98] [0.5719]
[-98] [0.9268]
[-98] [0.7205]
[-98] [0.7902]
[-98] [0.3668]
[-98] [0.8995]
[-98] [0.2396]
[-98] [0.7576]
[-98] [0.7577]
[-98] [0.4745]
[-98] [0.9994]
[-98] [0.6355]
[-98] [0.8145]

5 Commenti

Using a cell array slows things down... why not just return a numeric array?
I wish to store vectors in the first colum. Regards Sasanka
Dear Stephen,
Your tip had me thinking:
Actually, I could use a numeric array (instead of cell), if I give up on requiring the first column to have vectors.
The vectors I have in mind are row vectors with values 0 and 1, having (possibly) at most 300 to 1000 members.
I could potentially convert the vector into 1 number (of upto 1000 digits) by concatenating the members , as given by the code in below link.
col_1_member = sum(10.^(numel(config_v)-1:-1:0) .* config_v);
Question in my mind is: Am I protected from rounding errors? Will matlab correctly distinguish between a very large number and another number which is the previous number + 1? Also, If I use above code to concatenate vector members many times would there by a large(r) performance penalty?
Regards
Sasanka
Stephen23
Stephen23 il 21 Ott 2020
Modificato: Stephen23 il 21 Ott 2020
"Am I protected from rounding errors?"
Not with 1000 digits, that is well beyond the precision of any native numeric type. And converting to-from the digit representations will not be particularly efficient over your millions of iterations.
Would it be possible to return two outputs?:
  1. a cell array of numeric vectors
  2. a numeric array of random numbers
Dear Stephen,
I think what you say should be do-able in my situation.
(a). In the separate cell array of numeric vectors I shall keep my vectors. Cell (1,1) will have the first vector; Cell (1,2) will have the second vector; etc.
(b). I will have a separate (numeric) matrix with 2 columns (and N*max_periods rows): the second column shall have random numbers. The first col. shall have -98 in all rows at the beginning.
(c). At runtime, I will refer to the cell index number for the vector of my interest (in the separate cell array of numeric vectors - the numbers in bold 1, 2 above), and insert this row number in the first col. of the numeric array (replacing the -98 dummy value). This shall enable me to refer back, should I start from the numeric matrix and wish to know the vector a row (of tha numeric matrix ) corresponded to.
Thank you v. v. much!
Regards
Sasanka

Accedi per commentare.

Risposte (1)

KSSV
KSSV il 21 Ott 2020
Modificato: KSSV il 21 Ott 2020
function H_L1 = H2_GFL(N, max_periods)
num_rows = max_periods * N;
H_L1 = zeros(num_rows, 2, N);
rand_supply = rand(num_rows, N);
for k = 1:1:N
H_L1(:,2,k) = rand_supply(:,k);
H_L1(:,1,k) = -98; %% Dummy value, indicates end of data row
end
No loop required.
function H_L1 = H2_GFL(N, max_periods)
num_rows = max_periods * N;
H_L1 = zeros(num_rows, 2, N);
rand_supply = rand(num_rows, N);
H_L1(:,2,:) = rand_supply ;
H_L1(:,1,:) = -98 ;
end

1 Commento

Dear KSSV,
Thank you for the suggestion.
In the first column I shall be storing very large numbers or vectors. So, only using a numeric array is ruled out.
I am trying out the suggestion given by Stephen Cobeldick above. That may solve the problem.
Have a nice day!
Regards
Sasanka

Accedi per commentare.

Prodotti

Release

R14SP2

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by