Add vectors in loop

2 vues (au cours des 30 derniers jours)
Jørgen Fone Pedersen
Jørgen Fone Pedersen le 1 Mar 2021
How would one do something like this simpler? It works but i feel terrible for doing it this way and it takes up heaps of memory i guess
t_er = linspace(1,length(TimeSeries_norm),length(TimeSeries_norm));
ind1 = t_er >= ipts_shift(1) & t_er < ipts_shift(2);
ind2 = t_er >= ipts_shift(3) & t_er < ipts_shift(4);
ind3 = t_er >= ipts_shift(5) & t_er < ipts_shift(6);
ind4 = t_er >= ipts_shift(7) & t_er < ipts_shift(8);
ind5 = t_er >= ipts_shift(9) & t_er < ipts_shift(10);
ind6 = t_er >= ipts_shift(11) & t_er < ipts_shift(12);
ind7 = t_er >= ipts_shift(13) & t_er < ipts_shift(14);
ind8 = t_er >= ipts_shift(15) & t_er < ipts_shift(16);
ind9 = t_er >= ipts_shift(17) & t_er < ipts_shift(18);
ind10 = t_er >= ipts_shift(19) & t_er < ipts_shift(20);
indices = ind1+ind2+ind3+ind4+ind5+ind6+ind7+ind8+ind9+ind10;
TR_er = TimeSeries_norm' .* indices;
  2 commentaires
Allen
Allen le 1 Mar 2021
Jorgen,
Can you provide an example of the TimeSeries_norm and ipts_shift variables you are working with or preferably at least array dimensions/sizes?
Jørgen Fone Pedersen
Jørgen Fone Pedersen le 1 Mar 2021
Modifié(e) : Jørgen Fone Pedersen le 1 Mar 2021
Yes, sorry about that. Tried adding the .mat file but its too large.
The Timeseries_norm is just a normalized signal sampled at 1 Mhz. The ipts_shift is the indexes for start and stop in the timeseries for certain events i want to extract.
I multiply indices with timeseries later to zero out all points before index 1 and after index 2, before index 3 and after index 4 etc..
>> whos TimeSeries_norm ipts_shift
Name Size Bytes Class Attributes
TimeSeries_norm 80000000x1 320000000 single
ipts_shift 1x20 160 double
>>

Connectez-vous pour commenter.

Réponse acceptée

Allen
Allen le 2 Mar 2021
I am certain that there is a much better method using vectorized indexing, but without seeing an example of the values in TimeSeries_norm and ipts_shift, then a for-loop can get the job done fairly efficiently. @darova's method will work in generating an equivalent result for your indices variable, but needs a few corrections to the code in order to do so. Also, be aware that this method assumes that ipts_shift uses an even number of values, and that they are all paired values representing start/stop events.
indices = 0;
for i = 1:2:length(ipts_shift)
indices = indices + (ipts_shift(i)<=t_er & t_er<ipts_shift(i+1));
end
  1 commentaire
Jørgen Fone Pedersen
Jørgen Fone Pedersen le 3 Mar 2021
Thank you.
This works perfectly and looks so much better.

Connectez-vous pour commenter.

Plus de réponses (1)

darova
darova le 2 Mar 2021
Maybe for loop
ind = 0;
for i = 1:2:length(ipts_shift)
ind1 = ipts_shift(1) <= t_er & t_er < ipts_shift(2);
ind = ind + ind1;
end

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by