Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

# how to do vector

2 Ansichten (letzte 30 Tage)
Maria hassan am 14 Mär. 2018
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
Hi,
I have code to run but firstly my data should be balanced panel which I did. however, the second condition is that each variable a N*T vector which I do not know how to do. Could you please advise me how to do this task with thanks
Best regards
##### 2 KommentareKeine anzeigenKeine ausblenden
Birdman am 14 Mär. 2018
Maria hassan am 7 Apr. 2018
Bearbeitet: Walter Roberson am 8 Apr. 2018
Hi
Here is the code:my question is how I enter the series which contains around 10 series since I cannot name all the series same name.
function[x]=pd2(series,N,T)
psi=reshape(series,T,N);
z=psi(2:T,:)-psi(1:T-1,:);
x=reshape(z(2:T-1,:)-z(1:T-2,:),N*(T-2),1);
thanks

### Antworten (1)

John BG am 7 Apr. 2018
Hi Maria
the problem is the line
psi=reshape(series,T,N);
inside the function pd2.
reshape only works if the input matrix has exactly N*T elements.
To solve the uncertainty, you can do something like the following:
clear all;close all;clc
N=4;T=5;
series=randi([0 10],1,randi(N*T*2,1))
numel(series)
psi=0;
the previous is just to test it works.
Use something like these 2 ifs: if length of series shorter than N*T, then pad zeros or a value of your choice until you have N*T elements, and then use reshape.
If series has more elements than N*T you have make a decision, a possible policy could be take the N*T initial elements of the input series.
if numel(series)<=N*T % if series length shorter that N*T, add zeros up to N*T
psi=[series zeros(1,N*T-numel(series))]
psi=reshape(psi,T,N);
end
if numel(series)>N*T % if series length larger than N*T,
psi=series([1:N*T])
psi=reshape(psi,T,N);
end
thanks in advance for time and attention
John BG
##### 2 KommentareKeine anzeigenKeine ausblenden
Maria hassan am 7 Apr. 2018
Bearbeitet: Maria hassan am 7 Apr. 2018
Hi John,
my data is balanced data. I have 18*16=288. but How can I run the code I could not get it. so when I import the data, I import them as matrix and name the matrix as series so it work? I have run it and the results is column with 252 elements but elements from 238 till 252 are NaN. any idea why s that?
Regards Maria
John BG am 8 Apr. 2018
The NaN elements are not a problem for reshape, example
A=[1 NaN 3 7 -1 ; 0 NaN 5 8 11]
A =
1 NaN 3 7 -1
0 NaN 5 8 11
reshape(A,1,10)
=
1 0 NaN NaN 3 5 7 8 -1 11
you see? reshape knows it has to take then into account as if they were values.
As you are pointing out, the input matrix sometimes is not 288, but only 252.
This is why you have to fill up to 288 with more NaN elements, or zeros, or an element you choose convenient.
if numel(series)<=N*T
psi=[series zeros(1,N*T-numel(series))]
psi=reshape(psi,T,N);
end
to solve when the input is shorter than expected, to allow reshape to work correctly.
Use
if numel(series)>N*T
psi=series([1:N*T])
psi=reshape(psi,T,N);
end
to ignore the excess when the input is larger than the expected size.
Would it be possible for you to show the input data itself?

Diese Frage ist geschlossen.

### Community Treasure Hunt

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

Start Hunting!

Translated by