I'm trying to create a script that populates an array by loopung through each pulse frequency 0-50Hz and its duty cycle 0-100%
The sequence should look like this:
Freq Duty Cycle
1.00 0
1.01 0
1.02 0
...
1.00 1
1.01 1
1.02 1
...
1.00 2
1.01 2
1.02 2
I have the for
loop for the first part:
T = [];
num_of_values=0
for freq = 1:.01:2
num_of_values = num_of_values + 1;
T = [T; num_of_values freq]; %append to table
end
Which gives me:
1 1.00
2 1.01
3 2.02
...
Note: I added an increment in the beginning to see how many numbers are created.
I'm having trouble with attaching the duty cycle for
loop in the loop.
Also is there away to vectorize this to not use explicit loops?
I tried adding the duty cycle for
loop here but I don't get the output I'm trying to get:
T = [];
num_of_values = 0
for freq = 1:.01:2
num_of_values = num_of_values + 1;
for duty_cycle = 0:1:2
end
T = [T; num_of_values freq duty_cycle]; %append to table
end
You really shouldn't make a habit of reallocating an array of known size in a loop.
The first column is just the repetition of the frequency N times. Say N is 100, for each duty cycle. You can compute that with repmat
or kron
. The second column is the N duty cycles, each one repeated for the frequencies, which you can compute with kron
.
f = [0:0.01:50]';
ds = [0:100]';
m = numel(f);
n = numel(ds);
result = [repmat(f, n, 1) kron(ds, ones(m, 1))]