Seek: more efficient implementation
Show older comments
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 Comments
Stephen23
on 21 Oct 2020
Using a cell array slows things down... why not just return a numeric array?
Sasanka Sekhar Chanda
on 21 Oct 2020
Sasanka Sekhar Chanda
on 21 Oct 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?:
- a cell array of numeric vectors
- a numeric array of random numbers
Sasanka Sekhar Chanda
on 21 Oct 2020
Answers (1)
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 Comment
Sasanka Sekhar Chanda
on 21 Oct 2020
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!