InfluxDB provides movingAverage() or timedMovingAverage(), but what is the most efficient way to get the moving min/max?
For example, if I have data like:
_time
_value
01-01
-1
01-02
4
01-03
2
01-04
9
01-05
-4
01-06
3
01-07
8
01-08
1
The following would return:
|> movingMinMax(n: 3) or |> timedMovingMinMax(every: 3d, period: 8d)
_time
min
max
01-03
-1
4
01-04
2
9
01-05
-4
9
01-06
-4
9
01-07
-4
8
01-08
1
8
Related
This question already has answers here:
Building a matrix by merging the same row vector multiple times
(2 answers)
Closed 1 year ago.
I want to add the same data in different lines using Matlab. In my first version, I copied the data as shown in the code below.
Is there a better way to do that?
data=[-5:1:14;-5:1:14;-5:1:14;-5:1:14;]
answer:
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
You can try repmat
repmat(-5:14,4)
or kron
kron(-5:14,ones(4,1))
I have a fMRI data matrix, the size of which is 9*10 (I randomly put the value in it). The first two rows are under class 1 stimulus;the next two rows are under class 2 stimulus, the next next two rows are under class 3 stimulus, the last three rows are under no stimulus(rest condition). I want to test difference in signal between two conditions(class 1 stimulus vs rest condition), (class 2 stimulus vs rest condition) and (class 3 stimulus vs rest condition). My question is how to do T-test for the fMRI data?
H1: Condition1 ≠ Condition2
H0: Condition1 = Condition2
And should I compute based on these:1.Difference between the mean intensities of each condition
2. Degree of overlap in intensities
-7 0 -1 -5 -1 -2 -3 0 1 -8
2 -1 3 -1 -1 -1 -2 1 2 -3 ----> under class 1 stimulus
-4 -1 1 -1 8 1 0 -8 -2 -1
-2 -2 -5 -3 -1 -1 -15 0 -1 2 ----> under class 2 stimulus
3 0 5 8 -5 2 -2 8 10 -8
5 0 2 -4 8 2 6 0 -11 2 ----> under class 3 stimulus
-6 4 1 -2 6 -6 -5 0 11 -6
6 8 3 -4 -1 -5 5 -4 2 0
3 2 1 -6 -8 -4 2 0 5 3 -----> under rest (no stimulus) condition
It looks like you want to perform 2 sample (paired) t-test, in which case you want to use the ttest2 function. It's quite easy to compute: Without much information about your data I re-arranged them into single row vectors for comparisons.
The code I use is straightforward:
clear
clc
% Define experimental data.
Cond1 = [-8 2 -1 3 -1 -1 -1 -2 1 2 -3];
Cond2 = [-4 -1 1 -1 8 1 0 -8 -2 -1 -2 -2 -5 -3 -1 -1 -15 0 -1 2];
Cond3 = [3 0 5 8 -5 2 -2 8 10 -8 5 0 2 -4 8 2 6 0 -11 2];
Rest = [ -6 4 1 -2 6 -6 -5 0 11 -6 6 8 3 -4 -1 -5 5 -4 2 0 3 2 1 -6 -8 -4 2 0 5 3] ;
% Group data for easy referencing in plots
AllData = {Cond1;Cond2;Cond3;Rest};
% Perform the t tests. The p-value is given together with h, which tells you whether the null hypothesis is rejected (value of 0) or not (value of 1).
[h1,p1]=ttest2(Rest,Cond1)
[h2,p2]=ttest2(Rest,Cond2)
[h3,p3]=ttest2(Rest,Cond3)
PValues = [p1;p2;p3];
Plot the results
figure
for k = 1:4
if k < 4
subplot(1,4,k)
boxplot(AllData{k})
set(gca,'YLim',[-10 10])
TitleString = sprintf('Condition %i\n p-value of %0.2f',k,PValues(k));
title(TitleString,'FontSize',14)
else
subplot(1,4,k)
boxplot(AllData{4})
set(gca,'YLim',[-10 10])
title('Rest','FontSize',14)
end
end
Giving the following:
Is that what you meant? If not please provide more details about your data.
I want to centre a matrix to its mean,
A[i][j] = A[i][j]-mean(A,j)
So I subtract from each point the mean of the according column.
I could not find a function to centre my data, and it is not very straightforward to create my own
>> A=[1 4 7;2 5 8;3 6 9]
A =
1 4 7
2 5 8
3 6 9
>> A-repmat(mean(A),size(A,1),1)
ans =
-1 -1 -1
0 0 0
1 1 1
A = bsxfun(#minus,A,mean(A));
for example:
A = magic(5);
A = bsxfun(#minus, A, mean(A))
A =
4 11 -12 -5 2
10 -8 -6 1 3
-9 -7 0 7 9
-3 -1 6 8 -10
-2 5 12 -11 -4
I'm mighty confused about how to correctly apply the FFT transform and its inverse in Matlab. I have a program where I need to
apply FFT2 to a matrix of size 4x32 (corresponding to modes m=-1:2, n=-15:16)
do some processing, which leads to a coefficient matrix for another function, whose Fourier coefficients relate to the first set of data by a simple algebraic (component-wise) formula
Use some properties of the two functions that lets me calculate what I need by just summing up an expression on the form 2*abs(A_n)*cos(phi+n*theta+alpha_n) where A_n is the nth coefficient of the m=1 mode and alpha_n = arg(A_n).
I have experimented a little with the FFT2 function and tried to understand how it arranges its output. From what I understand (from sources in my course literature), the coefficients will be ordered as is illustrated by the following script:
>>m = -1:2; n = -7:8;
>>[N,M] = meshgrid(n,m);
>>MN = M; MN(:,:,2) = N;
>>asfft = #(X) [X(2:4,8:16,:) X(2:4,1:7,:); X(1,8:16,:) X(1,1:7,:)];
>>asfft(MN)
ans(:,:,1) =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
ans(:,:,2) =
0 1 2 3 4 5 6 7 8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7 8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7 8 -7 -6 -5 -4 -3 -2 -1
0 1 2 3 4 5 6 7 8 -7 -6 -5 -4 -3 -2 -1
where asfft reorders the indices in the same way I believe fft2 does, but does nothing else. In other words, each index is ordered from 0 up to the max, then from the min to -1. According to the documentation, I should be able to rearrange this so I get 0 in the middle by using fftshift, but it's not giving me the output I expect. Instead, I get this:
>> fftshift(asfft(MN))
ans(:,:,1) =
8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
ans(:,:,2) =
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
As you can see, the max is on the wrong side of the spectrum - instead of -7 -6 ... -1 0 1 ... 8 and -1 0 1 2 I have 8 -7 6 ... and 2 -1 0 1. This is fatal for me, since to be able to do the calculation described in 3 above, I need to know the indices of the respective coefficients. (The two layers are also switched, but that doesn't matter for me since I'm only going to do this on MxN matrices, not on N-d-arrays, later.)
Why is this? What am I doing wrong here?
Considering the simple 1D case first, fft gives you:
[X(0) X(1) X(2) ... X(N/2-1) X(-N/2) X(-N/2+1) ... X(-1)]
where I'm using X(k) to denote elements of the mathematical DFT.
All fftshift does is rotate these by N/2, so you end up with:
[X(-N/2) X(-N/2+1) ... X(-1) X(0) X(1) X(2) ... X(N/2-1)]
i.e. linear order.1
In the multi-dimensional case, fftshift simply applies this rotation in all dimensions.
1. Although since by definition, X(k) == X(N+k) for a DFT, the unrotated vector is also in "linear" order!
Check this newsread entry too
http://www.mathworks.com/matlabcentral/newsreader/view_thread/285244
taking a queue from Oli, the problem is that you're not parsing it in the middle:
so for asfft = #(X) [X(2:4,8:16,:) X(2:4,1:7,:); X(1,8:16,:) X(1,1:7,:)];
size(8:16)
ans =
1 9
size(1:7)
ans =
1 7
so do:
asfft = #(X) [X(3:4,9:16,:) X(3:4,1:8,:); X(1:2,9:16,:) X(1:2,1:8,:)];
I have sort of data,
A = [2 4 6 8 10]
B = [1 2 3 4 5 6 7 8 9 10]
How to write program that can subtracts each value of A from all values of B.
To better understand,
Take A = 2, subtract from all B = [1 2 3 4 5 6 7 8 9 10],
then take A = 4, subtract from all B = [1 2 3 4 5 6 7 8 9 10]
and so on...
If you want to create a new array C that contains, in row i the result of B-A(i), you use bsxfun:
A = [2 4 6 8 10];
B = [1 2 3 4 5 6 7 8 9 10];
C = bsxfun(#minus,B,A') %'#
C =
-1 0 1 2 3 4 5 6 7 8
-3 -2 -1 0 1 2 3 4 5 6
-5 -4 -3 -2 -1 0 1 2 3 4
-7 -6 -5 -4 -3 -2 -1 0 1 2
-9 -8 -7 -6 -5 -4 -3 -2 -1 0
If you want to create a new array C that contains the result of B-A(1)-A(2)-..., you write
C = B-sum(A)
C =
-29 -28 -27 -26 -25 -24 -23 -22 -21 -20