How to store iterated loop sine function in matrix?

I apologize because similar questions have been asked in the past, but I am very much a newbie to Matlab.
I am trying to store iterated results from an AM radio function in a for loop:
M=0.5;
A=0.5;
Fm=1;
Fc=100;
for x=0:0.0001:1
y=(1+M*sin(2*pi*Fm*x))*(A*sin(2*pi*Fc*x));
disp(y)
end
These results will need to be stored in a matrix format so that I can then plot the data as a histogram. Unfortunately, I am struggling to find a way to keep all values for y except the last result. Other suggestions for related topics (such as m(x:)=[x,y]) do not seem to work for me.
Thanks in advance for your help,
Sarah

 Respuesta aceptada

If you must use a loop, you need to index into it:
M=0.5;
A=0.5;
Fm=1;
Fc=100;
x=0:0.0001:1; % Create Vector
for k1 = 1:numel(x)
y(k1) = (1+M*sin(2*pi*Fm*x(k1)))*(A*sin(2*pi*Fc*x(k1)));
disp(y)
end
However if you wnat one vector for ‘y’ it is better to use vectorized code:
x=0:0.0001:1;
y=(1+M*sin(2*pi*Fm*x)).*(A*sin(2*pi*Fc*x));
Both produce the same result. The second is much faster and more efficient.

2 comentarios

This is great, thank-you so much, Star Strider! Agreed - both will work but the second does run so much faster.
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2018a

Preguntada:

el 27 de En. de 2019

Comentada:

el 27 de En. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by