Vectors problem
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 !!!
0 comentarios
Respuesta aceptada
Javier
el 27 de Jul. de 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 comentarios
Más respuestas (5)
Javier
el 27 de Jul. de 2011
I cannot understand well the problem,do you want to shift the first three elements of each vector?
Paulo Silva
el 27 de Jul. de 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 comentarios
Paulo Silva
el 27 de Jul. de 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)];
Javier
el 27 de Jul. de 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
Javier
el 27 de Jul. de 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?
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!