Matlab center a matrix to its mean - matlab

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

Related

Construct a matrix from a vector periodically [duplicate]

This question already has answers here:
How to create a symmetric matrix where each row/column is a subset of a known vector [duplicate]
(2 answers)
How do I generate the following matrix and vector from the given input data in MATLAB?
(1 answer)
Closed 5 years ago.
I have a vectorb with 30 entries.
I want to avoid using a for loop to construct a matrix like this:
where b_i is the i-th entry of the vector b.
For example, define the vector
b = [2 6 -7 3 1 -4 -1 1 11 8 -4 9 2 0 2 -1 0 4 4 4 2 -4 2 5 1 3 2 -1 1 -2]
where I tried by using for loop is:
A = zeros(5,5);
for i = 1:5
A(i) = b(i+5);
A(i+5) = b(i+6);
A(i+10) = b(i+7);
A(i+15) = b(i+8);
A(i+20) = b(i+9);
end
The result is
Is there a faster and more general method to generate this matrix?
You can use toeplitz:
A=fliplr(toeplitz(b(10:14),b(10:-1:6))
A =
-4 -1 1 11 8
-1 1 11 8 -4
1 11 8 -4 9
11 8 -4 9 2
8 -4 9 2 0
By the way, the indices here are 6 to 14 as in your example, and not 7 to 15 as in the picture. You can change it to your preferred purpose.
Your instinct is to avoid for loops. This is a good intuition to develop as a MATLAB programmer, but it's not always the quickest option. As can be seen in my answer to a very similar question, a for loop may be the quickest way to generate this type of matrix.
Possibly the shortest method to write would use hankel
A = b(hankel(7:11, 11:15));
Output:
>> ans =
-1 1 11 8 -4
1 11 8 -4 9
11 8 -4 9 2
8 -4 9 2 0
-4 9 2 0 2
Equivalent result (but quicker processing as seen in the previously linked answer)
A = hankel(b(7:11), b(11:15));
As Adiel said, there is a difference between the indices you showed in the image of the matrix and the indices you used to create your example. This uses the former.
Not as nice as using toeplitz, but a bit clearer to see what's going on:
b = [2 6 -7 3 1 -4 -1 1 11 8 -4 9 2 0 2 -1 0 4 4 4 2 -4 2 5 1 3 2 -1 1 -2];
n = 5; % window size
% StartIdx = 4; % Starting index of your window
AddVec = repelem(1:n,5)+StartIdx; % create addition vector
IdxVec = repmat(1:n,1,n); % Initialise index vector
IdxVec = AddVec+IdxVec; % add to let the window "slide"
c = b(IdxVec); % create a new vector
d = reshape(c,n,[]) % Reshape to get the desired matrix
d =
6 -7 3 1 -4
-7 3 1 -4 -1
3 1 -4 -1 1
1 -4 -1 1 11
-4 -1 1 11 8
Note that I didn't use a starting index in my run of the matrix. Adjust that parameter according to your needs.

How to calculate T test for my data matrix

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.

MATLAB Plot: modifying point position on y plane

I have this line of code in MATLAB, which sets these vectors:
x = [2 12 3 8 1 9 2; -3 -2 -1 0 1 2 3]
x =
2 12 3 8 1 9 2
-3 -2 -1 0 1 2 3
Considering the first row as points in y-plane and second row as x-axis in MATLAB plot
Now what line of code in MATLAB will take the maximum number in the first row and set at the middle(0) point in x-axis which will make it look like this
x =
9 2 2 12 3 8 1
-3 -2 -1 0 1 2 3
Please any idea is appreciated, I don't know how best to ask this question, I'm actually trying to edit a plot in MATLAB.
Code:
x = [2 12 3 8 1 9 2; -3 -2 -1 0 1 2 3];
[~,idx] = max(x(1,:));
x(1,:) = circshift(x(1,:),[0 (length(x)+1)/2-idx]);
Output:
x =
9 2 2 12 3 8 1
-3 -2 -1 0 1 2 3

How to split 1xN vector into MxL vector and fill the rest with zeros?

I have a matrix
a = [1 2 3 4 5 6 7 8 9 10 11]
I need to split it into rows of 5 and fill the rest of the unset block with zeros like this:
transformed = [ 1 2 3 4 5 ;
6 7 8 9 10;
11 0 0 0 0 ]
You could first expand a to have the required number of elements like this;
a(15) = 0 % Matlab will automatically fill elements 12:14 with 0
then
transformed = reshape(a,[5,3])'
produces
ans =
1 2 3 4 5
6 7 8 9 10
11 0 0 0 0

Minus each value of data

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