Splitting a vector up in to smaller vectors based on variance?
Mostra commenti meno recenti
Hi, I have a large number of microscopy image stacks that show the effect of different flow rates on a particle. I have used matlab to give me the angle of the particle at different times during the experiment, but its output is one vector of angles.
I would like to split this vector up, so that I can measure the standard deviation of the angles when no flow is applied, X flow is applied, no flow, Y flow, etc...
My main problem seems to be appending values onto a cell. Here, I'm trying to say that when the average of Angle(k) and it's three following angles is greater than 25, then Angle(k) does into Out{1}, if not it goes into a new cell Out{2}, and so on. Does anyone have any suggestion on how to do this?
for k=1:numFrames-4
if abs(Angle(k))+abs(Angle(k+1))+abs(Angle(k+2))+abs((Angle(k+3))/4 > 25;
Out{Indy} = ????;
else Indys = Indy+1;
Out{Indys}=????;
end
end
1 Commento
Stephen23
il 26 Set 2014
Is Angle a function or an array?
Risposta accettata
Più risposte (2)
For example you can perform abs on all of the Angle values simultaneously (if it is an array):
abs(Angle(k))+abs(Angle(k+1))+abs(Angle(k+2))+abs((Angle(k+3))/4 > 25;
can simply be performed all in one go:
mean(abs(Angle(k:k+3)))>25
(Assuming that your misplaced bracket really was intended to calculated the mean of all four values.)
This allows us to simplify the code:
if mean(abs(Angle(k:k+3)))>25
...
else
...
end
But in your case you might be able to use the indexing directly to allocate the values, without an if statement:
idx = mean(abs(Angle(k:k+3)))>25;
Out{2-idx} = Angle(k);
If you wish to accumulate more values on the same array within the cells, then you can append values:
Out = {[],[]};
for k=1:numFrames-4
idx = mean(abs(Angle(k:k+3)))>25;
Out{2-idx}(end+1) = Angle(k);
end
but it really would be better to preallocate the arrays in Out and then insert the values. Probably the best way to do this would be to use conv to calculate all of the means in one go, compare these values to 25, and then allocate to the output arrays using the comparison as indexing.
If all you want to do is measure the standard deviation according to your bins, I wouldn't bother splitting the array, and would just use accumarray:
angles = rand(1, 2000); %some values for demos
splitpoints = [200 450 750]; %some values for demos, indicates start of next bin.
splitlength = diff([1 splitpoints numel(angles)+1]);
bins = cell2mat(arrayfun(@(sp, idx) zeros(1, sp) + idx, splitlength, 1:numel(splitlength), 'UniformOutput', false));
binstd = accumarray(bins', angles, [], @std);
Otherwise, you can use mat2cell to split up your array any way you want and do whatever processing you want:
angles = rand(1, 2000); %some values for demos
splitpoints = [200 450 750]; %some values for demos, indicates start of next bin.
splitlength = diff([1 splitpoints numel(angles)+1]);
splitangles = mat2cell(angles, 1, splitlength);
binstd = cellfun(@std, splitangles);
edited for typos
Categorie
Scopri di più su Axis Labels in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!