how can I get a complete vector of residuals from an ARX model [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used ARX function then RESID function from the System Identification Toolbox, but the resulting residuals are:
0
0
0
5
6
8
7
8
the number of zeros=the number of lags, I need a complete vector of residuals

An AR model of order N needs the previous N values to predict the next one, which is why the first N are not predicted. You can always pad the vector at the beginning (either by replication or zeros), example:
load twotankdata
order = 5;
m = arx(y, order);
r = resid([y(1:order);y], m);
r = r(order+1:end);

Related

What is the meaning of a matrix exponent of a scalar in MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
For a scalar c and a nxnmatrix A, what is the meaning of c^A?
E.g.
>> c=2;
>> A=[1 2; 3 4];
>> c^A
ans =
10.4827 14.1519
21.2278 31.7106
All I could find was this section in the Mathworks documentation.
But what is the real meaning of that, is this a commonly accepted definition? Is this useful in real applications?
Any reference to a document with further details is appreciated.
(I am aware of the element-wise operation c.^A)

Is there a faster way to calculate the expected value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So for my probability class, my professor asked the following question on a homework problem:
A fair coin is flipped 10,000 times. Let X correspond to the difference between the number of heads and the number of tails. Using MATLAB, compute the expected value of X.
This is what I wrote to answer the question:
N = 10000;
i =0;
r=1/2;
Q=nchoosek(N,(X+N)/2);
X=(1,N);
for i=-N:N
P=Q*r.^(X+N)/2*(1-r)^(N-(X+N)/2) % probability_mass_function
E=sum(abs(X).*P); % expected value
end
However, is there faster and quicker way to compute this expected value? Any help would be greatly appreciated. Thanks
You can put all tests results in single matrix in one line and then calculate X per test, then average over X:
clear
TAIL=0; HEAD=1;
NumTests=121;
NumRollsPerTest=10*1000;
AllTestsRolls= rand(NumTests,NumRollsPerTest)>0.5 ; %head when rand>0.5
XperTest=sum(AllTestsRolls==HEAD,2)-sum(AllTestsRolls==TAIL,2);%every row is test so calc per test
ExpectedX=sum(XperTest)/length(XperTest)

How can I trace the highest value in a matrix until I go to a cell in the matrix with zero value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to start with the highest value in the matrix (i,j). Then, go backwards to one of positions (i-1,j), (i, j-1), and (i-1, j-1) depending on the direction of movement used to construct the matrix. This method is used throughout until a matrix cell with zero value is reached.The above image shows what I want, I need to trace those marked in blue. I know there is a max function in matlab.
If I understand what you try to do...
Suppose M is your matrix:
result=[];
[~,ind]=max(M(:));
[r,c]=ind2sub(size(M),ind);
result=[result; r c];
while M(r,c)~=0
[~,f]=max([M(r-1,c),M(r,c-1),M(r-1,c-1)]);
switch f
case 1
r=r-1;
case 2
c=c-1;
case 3
r=r-1;
c=c-1;
end
result=[result; r c];
end
Then, result is a 2 x n matrix, where each row is the indexes of one step- from the upper to lower.

How to compare two matrices on MatLab? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given a matrix A and a matrix B, how do I compare them element by element so the program returns a third matrix C that shows:
- If the element in A is larger than the one in B, the element in C should be 1.
- If the element in A is smaller than in B, the element in C should be -1.
- If the elements of both matrices are equal, the element in C should be 0.
Hope you can help!
C=zeros(size(A));
C(A>B) = 1;
C(A<B) = -1;
Note that it's never a good idea to do an equality test on floating point numbers.

Returning the last element of a vector with an unknown length [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I retrieve the last element of a vector only, provided that the length of that vector is unknown?
Use the special end keyword:
lastelement = myvector(end);
If the vector is called A, just use A(end).
In this case, use end, like #nispio and #David answered.
But it seems you think that not knowing the length can be a problem, but nope. That's because you can use length(v) if v is a column or row vector, or size(M) if M is a matrix.
Then, to get the last element of your vector, you could use (not recommended):
v(length(v)) if v is a row or a column vector
v(size(v,1)) if v is a column vector
v(size(v,2)) if v is a row vector
But if you use one of them, MATLAB will warn you:
The operation or expression <Indexing> has no evident effect.