for loop to remove corresponding vector values

조회 수: 1 (최근 30일)
Madlab
Madlab 2018년 10월 15일
댓글: madhan ravi 2018년 10월 16일
I need to edit the vector in 2 variables such that they correspond to each other.
For example,
y = 1 2 3 4 5 6
q = 8 26 1 5 1 6
Let's say I fix q and shift y by 1
y = 1 2 3 4 5 6
q = 8 26 1 5 1 6
I need vector y1 = 1 2 3 4 5 and q1 = 26 1 5 1 6
This means I need to kick out 6 in y and 8 in q respectively.
Let's say I fix q and shift y by 2
y = 1 2 3 4 5 6
q = 8 26 1 5 1 6
As before, now I need to remove 5,6 in y and 8,26 in q respectively.
I want to do this in a for loop as my vector is very long. Right now, I am struggling to get the vector right for q (which is my soundtwo) as shown below. Any tips?
% Creating time vector, "t"
t = linspace(0,16*pi,1000);
sound1 = 5*(cos(t) + 1*(rand(size(t))-0.5));
sound2 = 8*(cos(t) + 1.5*(rand(size(t))-0.5));
% Setting the time shift "dt"
dt = 1000;
% Creating a matrix to store product later on
list = zeros(dt,1);
% For loop for different shifts
for i=1:dt
% Now edit sound1 such that sound1 shifts while sound2 remains unchanged
%different time shift
sound1 = 5*(cos(t+ i ) + 1*(rand(size(t))-0.5));
sound2 = 8*(cos(t) + 1.5*(rand(size(t))-0.5));
% Shifting sound1
soundone = sound1(i:numel(sound1))
% Sound 2 unchanged, but have to assign respective vector to sound1
soundtwo = sound2()
multipliedsound = (soundone) .* (soundtwo);
add = sum(multipliedsound)
product = add / numel(t);
% Append product to list vector
list(i,1) = product;
end
  댓글 수: 2
Adam
Adam 2018년 10월 15일
편집: Adam 2018년 10월 15일
Just reverse ( flip ) your q vector and knock off the last 2, or however many you want to shift by, of both your y and q vectors. Then flip q again afterwards if the process stops at some point and you want your q vector back to normal.
I don't really understand how the code in the for loop relates to the example though. Where is the shift?
Madlab
Madlab 2018년 10월 16일
Thank you Adam, that was very helpful and I managed to do it with your tips.

댓글을 달려면 로그인하십시오.

답변 (1개)

madhan ravi
madhan ravi 2018년 10월 15일
y = [1 2 3 4 5 6]
q = [8 26 1 5 1 6]
y(y==5 | y==6)=[]
q(q==8 | q==26)=[]
You don’t need loop to do this even if the vector is so long.
  댓글 수: 2
Madlab
Madlab 2018년 10월 15일
ignore the y and q part please, it is just an example. The real code is below.
madhan ravi
madhan ravi 2018년 10월 16일
Oh just saw your comment

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by