Loop generation doubt.
显示 更早的评论
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
采纳的回答
更多回答(1 个)
the cyclist
2020-8-22
编辑:the cyclist
2020-8-22
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 个评论
Martin Thomas
2020-8-22
Martin Thomas
2020-8-22
the cyclist
2020-8-22
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
2020-8-22
Martin Thomas
2020-8-22
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!