Here's a simple question. The following nested for loop creates an array of sine-wave values.
N = 2^16;
for m = 1:10;
for i = 1:N
sine(m,i) = sin(2*pi*i./(8*2^m));
end
end
It seems like I should be able to create this array without the use of the for loops, but I've tried various syntaxes and always get an error message. Thanks in advance for any insights.
You could use bsxfun
like this:
sine = sin(bsxfun(@times, 2*pi*(1:2^16), 1./(8*2.^(1:10))' ));
Try the following:
ii = 1:2^16;
m = [1./(2.^(1:10))].'% transpose
prefactor = 2 * pi / 8;
sine = sin(prefactor * m * ii);
I perform a matrix multiplication A*B
, in which a is a column vector size nrows, and B is a row vector size ncols, the resulting matrix will be of size nrows x ncols. Therefore m
is a column vector and ii
a row vector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With