# Create a matrix from another one with an history window of 3 columns

1 view (last 30 days)
Julien Benistant on 10 Jan 2020
Commented: Julien Benistant on 13 Jan 2020
Could someone help me with my problem.
I've got an original 2 by 25 matrix and I want to create a new 2 by 25 matrix with the same columns than the original one. But I want them reorganized such that a given column (:,n) in the new matrix is different from the columns (:,n:n+2) of the original matrix.
I've struggled to do that as Ifind a way using loops that need a lot of if statement to control for special cases... Is there someone with a simple(r) method to do that?

Show 1 older comment
Julien Benistant on 10 Jan 2020
Here is an example,
a= [1,2,3];
b = [4,8,12];
A = combvec(a,b);
B = A(:,randperm(size(A,2)));
B =
1 2 1 3 1 2 3 2 3
12 8 4 4 8 4 8 12 1
What I wan't is a new matrix C to be equal to (C should be the same size than B but I shorten it for the example):
C =
3 1 3
4 12 12
with C(:,1) = B(:,4) being different from B(:,1:3), C(:,2) = B(:,1) being different from B(:,2:4) etc...
Hope the example is clear.
Stephane Dauvillier on 13 Jan 2020
OK? sorry I don't know what you mean
Julien Benistant on 13 Jan 2020
Sorry I wasn't clear but I found a simple solution, I post it here in case of someone need help:
A= [1,3,5,7,8];
B = [4,12,20,28,32];
C = combvec(A,B);
S = randi([4 21],1);
V = [C(:,S:end), C(:,1:S-1)];
Basically I've just created a new matrix (V) randomly lagged (S) compared to the original one (C) !
Best,

Stephane Dauvillier on 10 Jan 2020
You can try the function diff:
A = magic(5);
B = diff(A,[],2)
But B will have n-1 column what do you want for the extra column ?