Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 2 (최근 30일)
Derick Wong
Derick Wong 2013년 12월 19일
마감: MATLAB Answer Bot 2021년 8월 20일
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')];
  댓글 수: 2
Derick Wong
Derick Wong 2013년 12월 19일
Hi,
May I ask what if that the matrix has 343 columns and 93 rows ?
Andrei Bobrov
Andrei Bobrov 2013년 12월 19일
see in my answer code after row with "on Deric ..."

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