applying algebraic eqns across multiple dimesions of matrix using indexing
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sean Phillips
am 25 Apr. 2019
Kommentiert: Sean Phillips
am 26 Apr. 2019
I am "time-stepping" an electromagnetic field at time instances per frequency
The EM field value at time-zero is a 3D matrix of dimensions [nvalues x 1 x nfrequencies] (see attached)
I want to create a 4D matrix where time is the 4th dimension and apply an algebraic equation across each dimension based on the value of the variables stored in arrays [frequency] and [time]
ie
size(EM_freq_time) = [nvalues x 1 x frequencies x time]
to attempt this i create a matrix of zeros
EM_freq_time = zeros(length(Ex), length(frequencies), length(t));
the equation i must apply is
EM_value(frequency,time,phase) = value*sin(2*pi.*frequencies.*t+Phase)
where frequency is a column vector size [nfreqs, 1]
time is a column vector size [nsteps, 1]
phase is a column vector size [nvalues]
how can i apply this equation using indexing?
2 Kommentare
Bob Thompson
am 25 Apr. 2019
'how can i apply this equation using indexing?'
What exactly do you mean by this? Are you looking to run loops to examine different sets of values and want to index each set, or are you looking to get multiple sets at once using some kind of vector method? I would just appreciate some expansion on what exactly you're looking for.
As a side note, why do you have a 4D matrix when one of your dimensions is size one? There's nothing technically wrong with this, it just seems like making things '4D' unnecessarily complicates the problem.
Akzeptierte Antwort
Bob Thompson
am 25 Apr. 2019
Ok, you aren't too far off with your thoughts, but I'll include an example.
You should be able to cover the first and second dimension all at once, but because you want to look at specific values in the third and fourth dimensions you will need to loop them, as far as I know.
for i = 1:size(Ex,3) % Loop through each frequency
for j = 1:length(time) % Loop through each time value, not really sure where you're getting this from, but I'm also just setting this up as a template
Ex_freq_time(:,:,i,j) = Ex(:,:,i)*sin(2*pi*frequencies(i).*time(j)+Phase);
end
end
You may also need to index Phase (probably with i, because it matches the number of frequencies), but I wasn't entirely sure where you were getting it from. Sorry, I'm a very visual person, and this forum doesn't do pictures too well.
Let me know if you have any problems, or need clarification.
6 Kommentare
Bob Thompson
am 26 Apr. 2019
I believe the videowriter command allows you to save the video variable to an appropriate file.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Strategy & Logic finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!