Info

此问题已关闭。 请重新打开它进行编辑或回答。

How do I create such matrix ? (please look at the thread for further details)

2 次查看(过去 30 天)
Hi,
Let say I have a matrix [ X1 X2 X3 .... Xm]
I need to use a method base on X1, X2 and X3 to get my X4, X5 till Xm.
The method in order to find X4 is X4=(X1+X2+X3)/3. Once X4 is calculated, we use the X4 to calculate X5 which now the it will turn out to be X5=(X2+X3+X4)/3 and find X6 using the found X5 and X4, X6=(X3+X4+X5)/3 and so on until we find Xm.
My question is, how do we come out with such matrix ?

回答(3 个)

Walter Roberson
Walter Roberson 2013-12-19
X = rand(1,3);
for K = 4 : m
X(K) = mean(X(K-3:K-1));
end
  2 个评论
Derick Wong
Derick Wong 2013-12-19
That is useful,thanks. But what if now that there is an occasion where a matrix eg. [ 1 2 3 4 5 ; 1 2 3 4 5 ; 1 2 3 4 5] is to be the variable X u declared ? Given that matrix, I will have to recalculate X4 and X5 which is 4 and 5 in this case for all rows.
Walter Roberson
Walter Roberson 2013-12-21
Do you mean the case where X4 and X5 have already been found, so you want to continue on from X6 ? But 4 is not (1 + 2 + 3)/3 ?
If it is the question of how to do this for several rows simultaneously, then
X = rand(2,3); %example 2 rows
for K = 4 : m
X(:,K) = mean(X(:,K-3:K-1),2);
end

Andrei Bobrov
Andrei Bobrov 2013-12-19
编辑:Andrei Bobrov 2013-12-19
X = randi(25,1,10);
n = 3;
X = X(:);
X = [X(1:n);conv2(X(1:end-n+2),ones(n,1)/n,'valid')];
on Deric's comment
X = randi(1500,93,343);
n = 3;
X = [X(:,1:n), conv2(X(:,1:end-n+2),ones(1,n)/n,'valid')];

Roger Stafford
Roger Stafford 2013-12-21
In case it is of interest to you, Derick, here is an explicit formula for individual elements of your vector X in terms of its first three elements. That is, it doesn't involve iteration - one can find the n-th element without evaluating others.
Let x1, x2, and x3 be the first three elements.
a = (x1+2*x2+3*x3)/6;
b = (-x1+4*x2-3*x3)/6;
c = (-2*x1-x2+3*x3)/3/sqrt(2);
t = atan2(sqrt(2),-1);
X(n) = a+3^(-(n-2)/2)*(b*cos((n-2)*t)+c*sin((n-2)*t));
This shows that X consists of rather widely-spaced points in an exponentially decaying sine function.

此问题已关闭。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by