Inserting rows into matrix matlab - matlab

I have a ~ 100000/2 matrix. I'd like to go down the columns, average each vertically adjacent value, and insert that value in between the two values. For example...
1 2
3 4
4 6
7 8
would become
1 2
2 3
3 4
3.5 5
4 6
5.5 7
7 8
I'm not sure if there is a terse way to do this in matlab. I took a look at http://www.mathworks.com/matlabcentral/fileexchange/9984 but it seems to insert all of the rows in a matrix into the other one at a specific point. Obviously it can still be used, but just wondering if there is a simpler way.
Any help is appreciated, thanks.

Untested:
% Take the mean of adjacent pairs
x_mean = ([x; 0 0] + [0 0; x]) / 2;
% Interleave the two matrices
y = kron(x, [1;0]) + kron(x_mean(1:end-1,:), [0;1]);

%# works for any 2D matrix of size N-by-M
X = rand(100,2);
adjMean = mean(cat(3, X(1:end-1,:), X(2:end,:)), 3);
Y = zeros(2*size(X,1)-1, size(X,2));
Y(1:2:end,:) = X;
Y(2:2:end,:) = adjMean;

octave-3.0.3:57> a = [1,2; 3,4; 4,6; 7,8]
a =
1 2
3 4
4 6
7 8
octave-3.0.3:58> b = (circshift(a, -1) + a) / 2
b =
2.0000 3.0000
3.5000 5.0000
5.5000 7.0000
4.0000 5.0000
octave-3.0.3:60> reshape(vertcat(a', b'), 2, [])'(1:end-1, :)
ans =
1.0000 2.0000
2.0000 3.0000
3.0000 4.0000
3.5000 5.0000
4.0000 6.0000
5.5000 7.0000
7.0000 8.0000

Related

Solve System of Linear Equations in MatLab with Matrix of Arbitrary Size for Finite Difference Calculation

I am trying to write a script in MatLab R2016a that can solve a system of linear equations that can have different sizes depending on the values of p and Q.
I have the following equations that I am trying to solve, where h=[-p:1:p]*dx. Obviously, there is some index m where h=0, but that shouldn't be a problem.
I'm trying to write a function where I can input p and Q and build the matrix and then just solve it to get the coefficients. Is there a way to build a matrix using the variables p, Q, and h instead of using different integer values for each individual case?
I would use bsxfun(in recent matlab versions this function may be implented to the interpreter, I don't know for sure):
p = 4;
Q = 8;
dx = 1;
h = -p:p*dx
Qvector = [Q,1:Q-1]'
Matrix = bsxfun(#(Qvector, h)h.^(Qvector)./factorial(Qvector), Qvector, h)
Output:
h =
-4 -3 -2 -1 0 1 2 3 4
Qvector =
8
1
2
3
4
5
6
7
Matrix =
1.6254 0.1627 0.0063 0.0000 0 0.0000 0.0063 0.1627 1.6254
-4.0000 -3.0000 -2.0000 -1.0000 0 1.0000 2.0000 3.0000 4.0000
8.0000 4.5000 2.0000 0.5000 0 0.5000 2.0000 4.5000 8.0000
-10.6667 -4.5000 -1.3333 -0.1667 0 0.1667 1.3333 4.5000 10.6667
10.6667 3.3750 0.6667 0.0417 0 0.0417 0.6667 3.3750 10.6667
-8.5333 -2.0250 -0.2667 -0.0083 0 0.0083 0.2667 2.0250 8.5333
5.6889 1.0125 0.0889 0.0014 0 0.0014 0.0889 1.0125 5.6889
-3.2508 -0.4339 -0.0254 -0.0002 0 0.0002 0.0254 0.4339 3.2508

Calculating c mature in MATLAB. How can i solve it?

i have a `x=(t*n) stock return matrix, that n is number of stock in a portfolio and t is time. I want calculate
c=M{[x(it)-k(x)][y(it)-k(y)]}
where x(it)
return of stock i in time t
and the median M is taken with respect to the joint CDF of x(t)
and y(t), and k(x) and k(y) are the population medians of x(t) and y(t)
for example:
x=[1 2 3;6 7 5;3 5 6;7 8 9]
x =
1 2 3
6 7 5
3 5 6
7 8 9
t=size(x,1)
n=size(x,2)
medianx=median(x)
medianx =
4.5000 6.0000 5.5000
q=x-medianx(ones(t,1),:)
q =
-3.5000 -4.0000 -2.5000
1.5000 1.0000 -0.5000
-1.5000 -1.0000 0.5000
2.5000 2.0000 3.5000
I can do this to here and I don't know how can i reach c matrix in matlab. I calculate c manually that:
c =
4.2500 3.2500 4.0000
3.2500 2.5000 3.2500
4.0000 3.2500 3.2500
where
c(11)=median of(column1*colum1 of matrix q)=4.25
c(22)=median of(column2*colum2 of matrix q)=2.5
c(33)=median of(column3*colum3 of matrix q)=3.25
c(12) & c(21)=median of(column1*column2 of matrix q)=3.25
c(13) & c(31)=median of(column1*column3 of matrix q)=4
c(23) & c(32)=median of(column2*column3 of matrix q)=3.25
notice that i have a t*n matrix and matrix x just is a example. thanks
You can calculate c from q using this:
c=zeros(3,3); %Pre-allocation
for m=1:3
for n=1:3
c(m,n)= median(q(:,m).*q(:,n));
end
end

Reshaping a matrix

I have a matrix that looks something like this:
a=[1 1 2 2 3 3 4 4;
1.5 1.5 2.5 2.5 3.5 3.5 4.5 4.5]
what I would like to do is reshape this ie.
What I want is to take the 2x2 matrices next to one another and put them underneath each other.
So get:
b=[1 1;
1.5 1.5;
2 2;
2.5 2.5;
3 3;
3.5 3.5;
4 4;
4.5 4.5]
but I can't seem to manipulate the reshape function to do this for me
edit: the single line version might be a bit complicated, so I've also added one based on a for loop
2 reshapes and a permute should do it (we first split the matrices and store them in 3d), and then stack them. In order to stack them we first need to permute the dimensions (similar to a transpose).
>> reshape(permute(reshape(a,2,2,4),[1 3 2]),8,2)
ans =
1.0000 1.0000
1.5000 1.5000
2.0000 2.0000
2.5000 2.5000
3.0000 3.0000
3.5000 3.5000
4.0000 4.0000
4.5000 4.5000
the for loop based version is a bit more straight forward. We create an empty array of the correct size, and then insert each of the 2x2 matrices separately:
b=zeros(8,2);
for i=1:4,
b((2*i-1):(2*i),:) = a(:,(2*i-1):(2*i));
end

How to apply indices I got from one row to other rows in matlab?

Let say if I have this data
my_data = [ 10 20 30 40; 0.1 0.7 0.4 0.3; 6 1 2 3; 2 5 4 2];
my_index = logical(my_data(4,:)==2);
What is the simplest way to use 'my_index' to give this output
10.0000 40.0000
0.1000 0.3000
6.0000 3.0000
2.0000 2.0000
my_data(:,my_index)
but I'm suspicious that this is so simple that it doesn't satisfy your (background) requirements ...

how to delete decimals in Matlab matrix

I have an output like
a = [1 1.4 2.45 2.22; 2 3 4.2 1]
and I need the output to be like
[1 1 2 2; 2 3 4 1]
I don't want to round it.
fix maybe OK.
If you have both positive and negative numbers, and you just want to delete decimals, fix is a good choice.
b=[1 1.4 2.45 2.22; 2 3 -4.2 1]
b =
1.0000 1.4000 2.4500 2.2200
2.0000 3.0000 -4.2000 1.0000
fix(b)
ans =
1 1 2 2
2 3 -4 1
Use fix rather than round, e.g.
octave-3.4.0:1> a = [1 1.4 2.45 2.22; 2 3 4.8 1]
a =
1.0000 1.4000 2.4500 2.2200
2.0000 3.0000 4.8000 1.0000
octave-3.4.0:2> b = fix(a)
b =
1 1 2 2
2 3 4 1