Skip to content

Commit

Permalink
Revert "vectorized version of loops"
Browse files Browse the repository at this point in the history
This reverts commit d68876a.
  • Loading branch information
Karatsidis committed Dec 6, 2024
1 parent 23f5c2d commit 7899019
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions loops/octave/code.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
function code()
args = argv(); % Parse input arguments
u = str2double(args{1}); % Convert first input argument to double
% Get an input number from the command line
u = str2double(argv(){1}); % Convert input to double

r = randi([0, 10000]); % Get a random number 0 <= r < 10k
% Generate a random number between 0 and 10000
r = randi([0, 9999]);

mod_array = mod(0:99999, u); % Calculate j % u
inner_sum = sum(mod_array); % Sum the values
% Initialize an array of 10k elements to 0
a = zeros(1, 10000);

a = repmat(inner_sum, 1, 10000); % Initialize array of 10k elements to inner_sum
% Perform the nested loop operation
for i = 1:10000 % 10k outer loop iterations
for j = 0:99999 % 100k inner loop iterations, per outer loop iteration
a(i) = a(i) + mod(j, u); % Simple sum and mod operation
end
a(i) = a(i) + r; % Add a random value to each element in array
end

a = a + r; % Add the random value to each element

disp(a(r+1)); % display result (add +1 to maintain same behavior as Python)
disp(a(r + 1)); % Print out a single element from the array
end

code();

0 comments on commit 7899019

Please sign in to comment.