forked from bddicken/languages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "vectorized version of loops"
This reverts commit d68876a.
- Loading branch information
1 parent
23f5c2d
commit 7899019
Showing
1 changed file
with
14 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |