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

the cyclist
the cyclist el 22 de Ag. de 2020

0 votos

% DO NOT USE THESE. USE YOUR DATA. THESE ARE FOR AN EXAMPLE ONLY
T = rand(160547,1);
A = rand(44880,1);
k = rand(size(A));
w = rand(size(A));
%%%%%% Only use the code below %%%%%
% Preallocate memory for E
E = zeros(1,160547);
% Calculate E
for ii = 1:size(E2,2)
E(ii) = sum(A.*cos(k-(w.*T(ii))));
end

11 comentarios

the cyclist
the cyclist el 22 de Ag. de 2020
FYI, if your data weren't so large (which you did not mention in your original question), then
E = sum(A.*cos(k-(w.*T')));
which is just the sum of my original answer, would give the same result.
This code work even though the vector T is a different size, because of the transpose and the implicit expansion I mentioned. It would calculate the 160547x44880 array, as you state. I suggest you try it on a smaller dataset
Your data are too big, so that huge intermediate array cannot be stored. But here is proof that on a smaller dataset, it gives the same result:
% DO NOT USE THESE. USE YOUR DATA. THESE ARE FOR AN EXAMPLE ONLY
T = rand(1605,1);
A = rand(44880,1);
k = rand(size(A));
w = rand(size(A));
% Preallocate memory for E
E = zeros(1,1605);
% Calculate E
for ii = 1:size(E,2)
E(ii) = sum(A.*cos(k-(w.*T(ii))));
end
% Calculate using implicit expansion instead
E2 = sum(A.*cos(k-(w.*T')));
% Show that the two results are equal
isequal(E,E2)
the cyclist
the cyclist el 22 de Ag. de 2020
Editada: the cyclist el 22 de Ag. de 2020
You can attach your data file here, using the INSERT icon that looks like a paperclip.
Martin Thomas
Martin Thomas el 22 de Ag. de 2020
please find the attachment for cyclist.m is the code
Martin Thomas
Martin Thomas el 23 de Ag. de 2020
hope you understand that.
the cyclist
the cyclist el 23 de Ag. de 2020
Editada: the cyclist el 23 de Ag. de 2020
For my solutions above, it was important that your vectors were all row vectors, as you stated in your original question. In your actual code, they were not.
I believe the following aligns all the pieces correctly:
% Preallocate memory for eta
eta = zeros(160547,1);
% Precalculate K*X.
KX = K.*X;
% Calculate eta
for ii = 1:numel(eta)
eta(ii)=sum(amp'.*cos(KX-(f.*T_a(ii))));
end
The result I get is all NaNs, but I have not investigated why.
Martin Thomas
Martin Thomas el 23 de Ag. de 2020
same for me as well, im not good at coding, is any chance to sort it out. I realy need it that important for me
the cyclist
the cyclist el 23 de Ag. de 2020
eta is always NaN because K has a NaN in it (so the sum is always NaN).
The first element of K is NaN because it comes from 1/f, and the first element of f is zero.
The first element of f is zero because you defined it that way:
f = (Fs/2*linspace(0,1,nfft/2+1)).*(2*pi);
So, you need to fix that in a way that makes sense for your application.
Martin Thomas
Martin Thomas el 23 de Ag. de 2020
Let metry with
KX(isnan(KX))=0;
Martin Thomas
Martin Thomas el 23 de Ag. de 2020
hey got the result but i need to consult with my professor is that correct or wrong. Thank you so much. If anything i will ask you.
Martin Thomas
Martin Thomas el 23 de Ag. de 2020
hey one more doubt, i think you know the fft, in the fft amplitude plot from the above code you can see lot of peaks, moreover if i do the fft on any signal it will give me some more additional values is that possible to remove those values from that.
the cyclist
the cyclist el 23 de Ag. de 2020
I don't really use fft. I would open a new question, and upload an image of the plot using INSERT again, along with the code that generates it.
Try to be very clear about your exact question.

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 22 de Ag. de 2020
Editada: the cyclist el 22 de Ag. de 2020

0 votos

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
Martin Thomas el 22 de Ag. de 2020
Thanks for the reply,
is that possible to put the A=rand(FIRST ROW TO LAST ROW) without giving the actual value becauseA might have multiple same values.
Martin Thomas
Martin Thomas el 22 de Ag. de 2020
one more thing i have calculated A,k,w, t values already. i need to take that values itself.
the cyclist
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
Martin Thomas el 22 de Ag. de 2020
I have a time vector of
T=1:(1/128):1200 160547x1 double
A= fft results (amplitude) 44880x1 double
k=(same length of A) 44880x1 double
w= frequency (same lenth of A) 44880x1 double
The problem is T does not have equal length so i can't do the E. the only way i need to do is calculating first row of A,k and w with varying T will get a result same i need to be don in the second row, thrid until the end of the A,k and w value. then i wll sum it up
E = SUM(A.*cos(k-(w.*t')));
Martin Thomas
Martin Thomas el 22 de Ag. de 2020
So there willbe 160547x44880 or 44880x160547 matrix after summing up the values ther will be 160547x1 double matrix

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Ag. de 2020

Comentada:

el 23 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by