Vectors problem

3 views (last 30 days)
Susan
Susan on 27 Jul 2011
Hey Guys !!
I am working with vectors and I read about it before I post my question but can't still get it... Sorry if it is too basic but its difficult for me.. I have four vectors each of size 1x100.. x,y,g,z.. I did plot (x,y) and (g,z) and It is what I expect.. the four vectors are generated randomly so there is low chance the points values will be identical.. I want my first plot to be identical to my second but with leaving the first 3 points, to clear this part I mean usually points starts from point(1,1),(1,2),(1,3) etc but I shall leave those 3 points for the plot(x,y) and start from(1,4) and make it identical to the first point in plot (g,z) so x,y(1,4)= g,z(1,1) and so on.. so basically the two plots are now identical but the first is a shifted version.. I start breaking the steps to achieve this 1. since the vectors are separated in matlab workspace I thought first I need to concatenate the vectors together.. so I created A[x;y] 2x100 and B[g,z] 2x100.(Successful, I did that) 2. I looped through the points in B with ignoring the first 3 points of A and starting from(1,4) and copy the points.. first 3 points set it as NaN(Successful, I did that) 3.I need now to plot my vectors A,B first in two different plots then in same plot to see the result, here is my problem.. I thought by doing the first two steps I can easily plot A and B scale one down then I will be able to see that they the same but one is shifted but that didn't happen..
Is the way I broke down the problem wrong or my last step was not done properly???
Thanks all in advance !!!

Accepted Answer

Javier
Javier on 27 Jul 2011
See this and check if it's you're looking for:
function princ
lambdaMax=45;
T=50;
x=-2*pi:1/5:2*pi;
lambda =@(x)( sin(x)*(20+30));
S = Trial(lambdaMax,lambda,T)
hold on
% plot(S,lambda(S))
X = linspace(min(S),max(S),100);
Y = pchip(S,lambda(S),X);
Signal1=[X;Y]
plot(X,Y,'b')
lambdaFirst =@(x) (sin(x-4)*(20+30));
S1 = Trial(lambdaMax,lambdaFirst,T);
% plot(S1,lambdaFirst(S1))
X1 = linspace(min(S1),max(S1),100);
Y1 = pchip(S1,lambdaFirst(S1),X1);
Signal2 = [X1;Y1];
plot(X1,Y1,'r')%phase shitf
plot([zeros(1,4) X(1:end-4)],Y1,'black')%delay shift
% Signal1(1,1)= NaN;
% Signal1(1,2)=NaN;
% Signal1(1,3)=NaN;
% Signal1(1,4)=NaN;
% d = 4;
% for i=1:100-d
% Signal1(:,i+4) = Signal2(:,i);
% end
xlabel('t')
ylabel ('cos(x)')
legend('Original','Phase shift','Delay shift')
hold off;
end
function S = Trial(lambdaMax,lambda,T)
t = 0;
I = 0;
S = [];
u = rand;
t = t - log(u)/lambdaMax;
while t <= T
u=rand;
if (u < lambda(t)/lambdaMax)
I = I+1;
S(I) = t;
end
u = rand;
t = t - log(u)/lambdaMax;
end
end
  16 Comments
Susan
Susan on 27 Jul 2011
Can you please explain to me how you shift it and how you made both signals identical?.. I don't know how to test it but If you explain to me I will try it with different data?? figure(2);hold on;
subplot(3,1,1);plot(X,Y,'b');
subplot(3,1,2);plot(X1,Y1,'r')%phase shitf
subplot(3,1,3);plot([zeros(1,4) X(1:end-4)],Y1,'black')%delay shift
hold off;
Susan
Susan on 27 Jul 2011
and what is the difference between phase shift and delay shift?? I will be thankful if you just explain to me what you exactly did and how you achieved it !!

Sign in to comment.

More Answers (5)

Javier
Javier on 27 Jul 2011
I cannot understand well the problem,do you want to shift the first three elements of each vector?
  2 Comments
Susan
Susan on 27 Jul 2011
I did the shifting of the content of the vector but now I want to plot it and see the actually the both identical but one is shifted? I went through the steps I described above but the plotting seems issue since instead of x,y which are separate vectors I used A[x;y] because I need the pair of value to do the shifting.. Do you understand my problem now?
Susan
Susan on 27 Jul 2011
What to do you mean? Can you explain it a bit more,please?

Sign in to comment.


Paulo Silva
Paulo Silva on 27 Jul 2011
%generate data to work with
t=linspace(0,10,10); %time vector
v=rand(1,numel(t)); %vector with some random values
cla;hold on
plot(t,v)
n=3; %number of elements you want to remove from v
v=[v(n+1:end) nan(1,n)]; %remove the first n elements
%and put the same n number of NaN in the end of v
%so that the vectors remain with the same size
plot(t,v,'r')
legend('signal','signal shifted')
  6 Comments
Paulo Silva
Paulo Silva on 27 Jul 2011
n=3; %shift Signal1 n columns
Signal1(1,:)=[Signal1(1,n+1:end) nan(1,n)];
Signal1(2,:)=[Signal1(2,n+1:end) nan(1,n)];
Susan
Susan on 27 Jul 2011
I tried it still the graph is blank!!

Sign in to comment.


Javier
Javier on 27 Jul 2011
Well you has done a lot of question, I'll try to explain:
1ªwhat is the difference between phase shift and delay shift, phase shitf:is when you change the phase in a sine or cosine fase, e.g. sin(x)->hasn't got a gap in the phase
sin(x+-phi)->here, phi is a phase gap, due to you're adding or subtract the value phi, see this link http://en.wikipedia.org/wiki/Phase_(waves)#Phase_shift On the other hand, a delay in the singal means that the signal starts after a quatum of time, in your case 4seconds or samples depending on the units you are working with, the diference between this and the phase shift, is that all the signal is moved or shifted 4seconds/samples.
2ª Here,I've not done anything that you haven't done before, only I have plotted the ordinate of signal1 ,Y, on the abscissa of signal X, and with 'b' I command matlab to plot in blue
In this code:
lambda =@(x)( sin(x)*(20+30));
S = Trial(lambdaMax,lambda,T)
hold on
% plot(S,lambda(S))
X = linspace(min(S),max(S),100);
Y = pchip(S,lambda(S),X);
Signal1=[X;Y]
plot(X,Y,'b')
3ªIn this case,I have imposed the phase shift in the signal, notice that if I had set sin(x+4)*(20+30), the shitf will be to the left. Then I used this variable with your code and lastly with the new axe values(X1,Y2) plot again,here I have not to touch the 'x' axe because I have modified it in the sine expression,so it isn't necessary do it again. With 'r' command matlab to plot in red the singal.
lambdaFirst =@(x) (sin(x-4)*(20+30));
S1 = Trial(lambdaMax,lambdaFirst,T);
% plot(S1,lambdaFirst(S1))
X1 = linspace(min(S1),max(S1),100);
Y1 = pchip(S1,lambdaFirst(S1),X1);
Signal2 = [X1;Y1];
plot(X1,Y1,'r')%phase shitf
4ªThe delay of the signal is done with the command below,as you can imagine before 4s(I'm going to asume time units) there is not delay signal due to the fact that it hasn't arrived yet, so the values of x axe should be zero, for that I use the command "zeros(n,m)" which set a matrix of nxm zeros elements.After this 4seconds there is singal, so I'have copied the values of the x-axe Singal1 given that we want to be the same in signal2, so as to do this I copy the values of X, but realizing that if we want to draw with plot, the vector must have the same length, so I have to subtract the 4 last values of the X axe and that is done with "X(1:end-4)", I'm telling matlab to take the values of X from 1 to last value less the 4 last. Finally we have to get together the first four values of x(which ones were zero) and the rest, and that we make with the "[ ]", this way our x axe will be:
xDelay=[zeros(1,4) X(1:end-4)];
plot(xDelay,Y1,'black')%delay shift
or in one sole command as I've used:
plot([zeros(1,4) X(1:end-4)],Y1,'black')%delay shift
I suggest that you read the documentation of "plot" to have a clear idea of this command, you can do that by typing "help plot" on matlab prompt
Regards
  1 Comment
Susan
Susan on 27 Jul 2011
Yeah your right, I pretty much did it, it was the plot causing me trouble.. I always do the main and end up sorting out something that should be easy but will definitely read the documentation regarding the plot.. Thanks a lot for your time and effort.. Really appreciate it :)

Sign in to comment.


Javier
Javier on 27 Jul 2011
So you want to do something like this:
xy=[ 1;1
2;2
3;3
4;4
5;5
6;6
7;7]
gz=[ 8;8
9;9
10;10
11;11
12;12
13;13
14;14]
You make the shift and gets something like:
xy=[ 1;1
2;2
3;3
4;4
8;8
9;9
10;10
...]
gz=[ 8;8
9;9
10;10
11;11
12;12
13;13
14;14]
and now what do you want to plot?
  1 Comment
Susan
Susan on 27 Jul 2011
I explained below with my code.. I am too confused !!

Sign in to comment.


Susan
Susan on 27 Jul 2011
I am well confused to be honest, I think the way I broke down the problem is logical but still can't figure out how to plot it. @Javier I don't get what you trying to do, @Paulo your small piece of code worked when I run it and was positive I will be able to make it work in mine but It didn't happen :( !! I will post my code so You can see what exactly I am talking about!!! Trial simply generates the random numbers and the Script is below to run it..
function S = Trial(lambdaMax,lambda,T)
t = 0;
I = 0;
S = [];
u = rand;
t = t - log(u)/lambdaMax;
while t <= T
u=rand;
if (u < lambda(t)/lambdaMax)
I = I+1;
S(I) = t;
end
u = rand;
t = t - log(u)/lambdaMax;
end
lambdaMax=45;
T=50;
x=-2*pi:1/5:2*pi;
lambda =@(x)( sin(x)*(20+30));
S = nonhomogeneousPossion(lambdaMax,lambda,T)
hold on
plot(S,lambda(S))
%plot(S,lambda(S))
xlabel('t')
ylabel ('cos(x)')
X = linspace(min(S),max(S),100);
Y = pchip(S,lambda(S),X);
Signal1=[X;Y]
plot(X,Y)
lambdaMa = 45;
T=50;
x=-2*pi:1/5:2*pi;
lambdaFirst =@(x) (sin(x)*20+30);
S1 = nonhomogeneousP(lambdaMa,lambdaFirst,T);
plot(S1,lambdaFirst(S1))
xlabel('t')
ylabel ('cos(x)')
X1 = linspace(min(S1),max(S1),100);
Y1 = pchip(S1,lambdaFirst(S1),X);
Signal2 = [X1;Y1];
Signal1(1,1)= NaN;
Signal1(1,2)=NaN;
Signal1(1,3)=NaN;
Signal1(1,4)=NaN;
d = 4;
for i=1:100-d
Signal1(:,i+4) = Signal2(:,i);
end
  10 Comments
Susan
Susan on 27 Jul 2011
They should be identical but just a bit of delay and I thought by making the first 3 points NaN will do it.. If u see d=4 and the loop is shifting the content of the second signal into the first signal starting from Signal(1,4)=Signal(1,1)... Do you have an idea how to achieve this by looking what I am doing in my code ?? I am too confused about this???
Susan
Susan on 27 Jul 2011
So eventually I should have the first signal plotted and the second one(Its identical coz I copied all its content to the first starting from (1,4))plotted underneath and It will be obvious that they are both identical but one is delayed a bit!!!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!