Indexing with different sized vectors
Afficher commentaires plus anciens
I currently have one vector (49x1) which contains time stamp values - i.e. the first 3 numbers of the vector idx = [1;2013;5045] are associated with specified points in time. I also have another vector (X) which has numerical values for each given point in time - i.e. if I want to the find the value at the specified time point I can just index straight into this. However, I want to find the mean of subsequent points from the initial timestamps specified above and over differing durations. For example I would like to find the mean of the 200 points after the first time stamp and the mean of 350 points after the second timestamp. Essentially I have a third vector - duration = [200;350;255] - specifying the durations for which I want to calculate.
I have currently got the function set up but only returns one value as opposed to one for each idx:
function MeanX = AvgValues(X,Idx,duration)
Duration = Idx + duration;
MeanX = mean(X(Idx:Duration,:),1,'omitnan');
end
6 commentaires
@William Sheehan: note that MATLAB is case sensitive, so your code will have two similarly named variables duration and Duration. I recommend avoiding using names that only differ by character case.
"I currently have one vector (49x1) which contains time stamp values..."
"I also have another vector (X) which has numerical values for each given point in time..."
Does this mean that XC also has 49 elements? If so, how can you average 200 or 350 elements of an array with only 49 elements?
Your explanation is not clear. Perhaps a simple numeric example, with both input and output vectors would be useful to show us what you want.
William Sheehan
le 13 Août 2018
Modifié(e) : dpb
le 13 Août 2018
William Sheehan
le 13 Août 2018
dpb
le 13 Août 2018
How about attaching a small(ish) .mat file with sample data, William? I can't quite wrap my head around what the actual values are; the expression Idx(k):duration carries an implied differential; is that reliable?
It may not in the end be any faster than the explicit loop but I'm pretty sure a variation on the Answer is possible or with some of the other more recent features for grouping.
I thought the IDX variable was a list of indices from the initial posting; I see that isn't so altho I wonder if it wouldn't be more efficient to find those from the combination; are the start:end points contained in the X vector or can the computed endpoint above be between actual data values?
Note that this does not make much sense:
Output = zeros(size(length(Idx)))
length returns a scalar. The size of a scalar is 1x1. So that line is equivalent to
Output = zeros(1,1);
or even simpler:
Output = 0;
How many columns does X have?
Output = zeros(size(length(Idx)))
should be
Output = zeros(size(Idx));
and there's no need for the secondary Count indexing variable; it's the same as k so mayst as well just use it...
function Output = Val(X,Idx,Dur)
Output = zeros(size(Idx));
for k = 1:length(Idx)
duration = Idx(k)+(Dur);
t = Idx(k):duration;
Output(k) = nanmean(X(t,:));
end
end
Réponses (1)
arrayfun(@(ix,n) mean(X(ix:ix+n-1),'omitnan'), Idx,Duration);
Alternatively, instead of generating the start index and count, build a grouping variable and use findgroups, splitapply or discretize...actually, iiuc, you may have the bin edges already for the latter with your indexing vector.
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!