-1

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
MatCode
  • 114
  • 4
  • 14
  • It is very unclear what you are asking. The answer to the first part, before the code is `y2=y(1:end-shift); q2=q(shift:end)` I do not understand how the code is related to this though. – Ander Biguri Oct 15 '18 at 15:23
  • ignore the y and q part please, it is just an example. The real code is below. – MatCode Oct 15 '18 at 15:46
  • If you want us to ignore the `q` and `y` part, why did you include it in the question? I understand what you are asking in the `q` and `y` part, but I don't understand the code, how that relates to removing elements from the arrays. It looks like you're trying to compute the cross correlation between `sound1` and `sound2`, except that you're adding different noise for every shift, which is weird. To compute the cross-correlation, use `conv`. Or use `fft` [as here](https://stackoverflow.com/questions/7396814/cross-correlation-in-matlab-without-using-the-inbuilt-function). – Cris Luengo Oct 15 '18 at 16:49
  • @MatCode please only add relevant information about your problem – Ander Biguri Oct 15 '18 at 17:48

1 Answers1

1

The function circshift will rotate the values of y, you can then set the rotated values of NaN. Setting the rotated values of y to NaN removes the need to also change or remove values in q: The product values with NaN will also be NaN, which are then ignored by NaN sum. The example below shifts the values by 2 but you can easily substitute in other shift values.

y = [1 2 3 4 5 6];
q = [8 26 1 5 1 6];

shift_value = 2;

y_shifted = circshift(y, shift_value);
y_shifted(1:shift_value) = NaN;

product_value = y_shifted .* q;
sum_value = nansum(product_value);
DMR
  • 1,479
  • 1
  • 8
  • 11
  • 1
    Why insert NaN and then use `nansum`, rather than just removing the elements you don't need, [as suggested by Ander](https://stackoverflow.com/questions/52819892/for-loop-to-correlate-and-edit-vectors#comment92557197_52819892)? – Cris Luengo Oct 15 '18 at 16:51
  • I had suggested this method so only the 'y' vector would need to be manipulated, but either would work. – DMR Oct 15 '18 at 17:42
  • Ah, that makes sense. Good idea! Note that since R2018b (or was it R2018a?) you can do `sum(A,'omitnan')`, if you don't have `nansum` (it requires a toolbox). – Cris Luengo Oct 15 '18 at 17:51