0

I have a column vector and I am trying to write a function implementing somehow a variable windowing function. Meaning that i want to choose one row and skip a number of rows (this is the variable part), but not only skipping, i have also to set the value of one of the columns in the skipped row equal to the chosen row before them of the same column. The column is:

----------
  P1
----------
  P2
----------
  P3
----------
  P4
----------

So the goal is to create a new column with P1 P1 P3 P3 P4 P4 ... The variable part means by changing a variable in the function, it is possible to create a new column with P1 P1 P1 P4 P4 P4 P7 P7 P7 ...

I tired something like this:( to implement the first case)

    % column vector containing P values a
    a ;

    delay = 0;
     % f parameter to enter the delay processing
    f = 2;

    r = length(a);
    i = 1;
   while(i <= r)
    if(mod(i, f) == 0)
        for j = 0 : delay
            a(i + j) = a(i - 1);
        end
        i = i + delay + 1;

     else
        i = i + 1;
     end
     end

I think the problem is in using the MOD function or choosing the values of f.

any help is appreciated.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
user36219
  • 1
  • 1

2 Answers2

0

Answer is the following, including a comparison between windows and saving all results in an array:

a = v;
r = length(a);
i = 1;
all_Results = [];
Vectors = [];
for window =1:128 ;

while(i < r)

        for w = 1 : window;
            if (i < r )
            i = i+1;
            a(i ) = a(i-1);
            end
        end
        i = i +  1 ;    
end
equal_elements = length(find(a==t));
accuracy = equal_elements/ length(t);
Results = [ window , accuracy ];
Vectors = [Vectors , a ];
all_Results = [all_Results; Results];
a = v;
i = 1;
end 
Autonomous
  • 8,935
  • 1
  • 38
  • 77
user36219
  • 1
  • 1
0

Here my suggestion. A bit simpler I think. To bring the index vector in the right shape I borrowed the solution form A similar function to R's rep in Matlab. Which is quite similar to your question.

%# random vector, parameter i to enter the delay processing
vector = rand(1000000,1);
i=2;

%# entries of vector to be duplicated
repeat = 1:i:length(vector);

%# fill gaps in this index vector and cut to same length
matrix = repmat(repeat,i,1);
index = matrix(:);
index(length(vector)+1:end) = [];

%# generate desired result
vector = vector(index);

Timing for those parameters on my machine: Elapsed time is 0.055114 seconds.

Community
  • 1
  • 1
FxH
  • 44
  • 4