How to find the difference between two vectors

Hello all,
My doubt is,
I have to find the difference between two vectors( for eg: y(8) and y(4) or y(6) and y(2)) and have to append the difference output also get the appended output in plot. So could anyone please tell me how to do these functions?.
y_try=[0 1e-9 0 0 0 1e-9 0 0] in this
y(1) =[0] ; y(2) = [1e-9] ; y(3) =[0] ; y(4) =[0]
y(5) =[0]; y(6) = [1e-9]; y(7) = [0] ; y(8) = [0]

3 Comments

Do you mean like
diffy = y(8) - y(4)
diffy = y(6) - y(2)
diffy = y(someIndex) - y(someIndex - 4)
Then you want to append the difference to what?
I have to find the difference of output for each vectors (y(8) and y(4) , y(6) and y(2), y(7) and y(3), y(5) and y(1)) and append all these difference output and plot as a figure.
Thank you for your response. The difference is working and now I have problem with appending.

Sign in to comment.

 Accepted Answer

Well, this is a guess, but it appears that maybe you have x,y data strung together in a single vector and want to separate out the two and difference -- try
y_try=[0 1e-9 0 0 0 1e-9 0 0];
d=diff(reshape(y_try,[],2),[],2);
plot(d)
Of course, with the above dataset the differences are all identically zero; this may be just owing to the particular values used as example or, it could be a complete misunderstanding of the intent of the question...

10 Comments

Thank you your response.I tried the code, but its showing the error 'Index in position 2 is invalid. Array indices must be positive integers or logical values'.
It (@dpb's code) does not. He ran it above in MATLAB online (and I double checked it) and you can see it ran perfectly.
If yours does not work it's because you changed something.
For your error, see the FAQ:
or attach your actual code if you want us to fix it.
Well, as shown, the code ran on your example data so it's not the code that's the problem.
You'll have to show us the actual data and code you ran...
Sorry I am new to this platform, Could you please tell me, is it correct to attach the actual code here or shall I send it personally?
Sure, it's expected. Paste the text of the code and then use the "Code" icon (or Ctrl-E) to format it.
Attach the data needed as well, and show the error in its entirety as well.
Small sample sizes such as your first posting are all that are needed as long as they illustrate the issues of what is the real problem to be solved.
In your case, undoubtedly more explanation of just what these data are and how they are obtained and whether my assumption of how they were created in the form I assumed is at all representative of the real problem or not would be invaluable.
While that explanation explains the original posting data and results in the apparent answer for those data; it seems a very peculiar way to have data collected and one suspects that may not be the real case at all...
function cantilever_trialadd
global count
global max
global env
global damping1
global damping2
global omega_0_square1
global omega_0_square2
global freq_sound
global F_ext
global alpha
global beta
global gamma
global a_f1
global a_f0
global R
global u_dc1
global u_dc2
global kalib1
global kalib2
global freq_act
global u_ac
global tau
global a_flist
global T_list
global difference_list
global difference_aflist
global flag
global t0
global sampling
R=15;
freq_res1= 1000;
freq_res2=1050;
bandwidth1 = 100;
Q1 = freq_res1/bandwidth1;
bandwidth2 = 150;
Q2 = freq_res2/bandwidth2;
omega_0_1=freq_res1*2*pi;
omega_0_2=freq_res2*2*pi;
omega_0_square1=omega_0_1^2;
omega_0_square2=omega_0_2^2;
damp1=1/(2*Q1);
damp2=1/(2*Q2);
damping1=damp1*2*omega_0_1;
damping2=damp2*2*omega_0_2;
alpha=19.2;
beta=1.0066e+03;
gamma=1.6214e+07;
kalib1=1e6;
kalib2=2e6;
u_dc1=-0.2;
u_dc2=-0.1;
a_f0 = 0;
tau=0.001;
F_ext=0.5;
freq_sound=1000;
t_final = 0.2;
zeit = [0 t_final];
y_start2=[0 1e-9 0 0 0 1e-9 0 0];
options = odeset('MaxStep',1e-6);
a_f1 = 0.3;
[T1,Y1] = ode15s(@(zeit,y_start2) cantimodel_temp(zeit,y_start2),zeit,y_start2,options);
figure;
plot(T1,Y1(:,2),'r--');
hold on;
plot(T1,Y1(:,6),'b--');
legend('canti1','canti2');
hold off;
end
function dy = cantimodel_temp(t,y)
global damping1
global damping2
global omega_0_square1
global omega_0_square2
global freq_sound
global F_ext
global alpha
global beta
global gamma
global tau
global a_f0
global a_f1
global R
global u_dc1
global u_dc2
global kalib1
global kalib2
global env
global max
global count
global t0
dy(1) = -beta.*y(1)+gamma.*(tanh(a_f0.*kalib1.*y(4)+u_dc1)/R)^2;
dy(2) = y(3);
dy(3) = -damping1*y(3)-omega_0_square1*y(2)+alpha*y(1)+F_ext*sin(2*pi*freq_sound*t);
dy(4)=(-y(4)/tau+y(3));
dy(5) = -beta.*y(5)+gamma.*(tanh(a_f1.*kalib2.*y(8)+u_dc2)/R)^2;
dy(6) = y(7);
dy(7) = -damping2*y(7)-omega_0_square2*y(6)+alpha*y(5)+F_ext*sin(2*pi*freq_sound*t);
dy(8)=(-y(8)/tau+y(7));
dy=dy';
diff=(y(8)-y(4));
d=diff(reshape(y_start2,[y(8)],2),[y(4)],2); %(I have inserted a code here)
t(end);
end
Hello ,
I have a attached the code above. In this, I have to find the difference between two vectors (y(8) and y(4) , y(6) and y(2), y(7) and y(3), y(5) and y(1)) and append the difference outputs together and output should be in the form of graph.
Thank you, Vani Muthu.
Well, in
d=diff(reshape(y_start2,[y(8)],2),[y(4)],2); %(I have inserted a code here)
you didn't even bother to switch the variable name to the variable you used in your code instead of that of the original example.
But even before you got there, in
diff=(y(8)-y(4));
you aliased the MATLAB builtin diff() function with a local variable so broke the use of diff as a function. Get rid of that line entirely and don't use builtin function names as variables; "There be dragons!"
...
dy(1) = -beta.*y(1)+gamma.*(tanh(a_f0.*kalib1.*y(4)+u_dc1)/R)^2;
dy(2) = y(3);
dy(3) = -damping1*y(3)-omega_0_square1*y(2)+alpha*y(1)+F_ext*sin(2*pi*freq_sound*t);
dy(4)=(-y(4)/tau+y(3));
dy(5) = -beta.*y(5)+gamma.*(tanh(a_f1.*kalib2.*y(8)+u_dc2)/R)^2;
dy(6) = y(7);
dy(7) = -damping2*y(7)-omega_0_square2*y(6)+alpha*y(5)+F_ext*sin(2*pi*freq_sound*t);
dy(8)=(-y(8)/tau+y(7));
dy=dy(:); % make it a column vector
d=diff(reshape(dy,[],2),[],2); % reshape to 2 columns and subtract the two
will return the difference vector you're asking for, but it's only going to be for a single point in time and am not at all sure what you think it means to have done so.
You'll have to figure out from your application how to return it to the caller and what to try to do with it; I'm at a loss to have a klew beyond simply computing what it is you've asked for, sorry.
Thank you so much for your response. Your code helped a lot to rectify the error.
Glad to help where can...even though sometimes we don't have enough information to have any idea what the result means, can still at least fix syntax errors or find the correct command for the job.
If the above resolved the problem, please go ahead and "Accept" the answer if or no other reason than to let others know it's not still an outstanding issue.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2022a

Tags

Asked:

on 12 Oct 2022

Commented:

dpb
on 14 Oct 2022

Community Treasure Hunt

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

Start Hunting!