Loop generation doubt.
Mostrar comentarios más antiguos
Hi,
I need to create a loop. I have to do a numerical analysis of thousands of data points.
My equation is,
E=A*cos(k-(w*t))
I have A,k and w three of them have same length 1x10. In the case of t which i have 1x50.
what i have to do is i need to take the first value of A,k and w and vary t (means run with range of values) . I need to do the same processs for the rest of the rows of A,k and w and vary t.
To be more precise.
A k w T
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
N N N 5
6
N
the equation will be
First calculation need to be
E=1*cos(1-(1*(1....N)))
second
E=2*cos(2-(2*(1....N)))
.......
continue upto length of A,k,w
Respuesta aceptada
Más respuestas (1)
the cyclist
el 22 de Ag. de 2020
Editada: the cyclist
el 22 de Ag. de 2020
You don't need to use a loop. MATLAB will do vectorized calculations.
If you have a fairly recent version of MATLAB, you can do this using implicit expansion of the vectors:
% Make up some data
A = rand(1,10);
k = rand(1,10);
w = rand(1,10);
t = rand(1,50);
% Calculate E
E = A.*cos(k-(w.*t'));
The output E will be 50x10.
Note that I took the transpose of t, and also used element-wise multiplication and not matrix multiplication. You can read about the difference here.
5 comentarios
Martin Thomas
el 22 de Ag. de 2020
Martin Thomas
el 22 de Ag. de 2020
the cyclist
el 22 de Ag. de 2020
The only code you need is the last line I wrote:
% Calculate E
E = A.*cos(k-(w.*t'));
The other lines of code are just making up some pretend input values, which I used to make sure that last line worked. You should just use your values for A, k, and w.
I didn't really understand your other comments, so if that doesn't help, maybe try to explain it again?
Martin Thomas
el 22 de Ag. de 2020
Martin Thomas
el 22 de Ag. de 2020
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!