0

Within Matlab I need to duplicate every odd row in an array excluding the first and last, for example:

g =  [5;    6;     11;     12;     17;   23;    24;     29;   30];

the code will manipulate it in to:

g =  [5;     6;     11;    11;    12;    17;    17;   23;    24;   24;     29;   30]

How can I generate this new sequence?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120

3 Answers3

2

There is a nasty way of making this array. I'm saying it's nasty because it's hard to read what is going on, please put a lot of comments around this bit of code if you use it!

You want to create the set of indices 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, ..., repeating the odd ones (except 1, and also make sure the last one is not repeated). The sequence of repeated odd values is really the floor or the sequence 1, 2/3, 4/3, 6/3, 8/3, ..., which is easy to generate: 1:2/3:N. This creates the sequence 1, 1, 2, 3, 3, ..., we need to remove the first element.

It turns out (by experimenting) that the index N doesn't get repeated if N is odd (as desired) but gets skipped if N is even. So we generate the index sequence up to N+1, and remove the last element. This way, N is not repeated if odd, but is always there.

So we have:

g =  [5; 6; 11; 12; 17; 23; 24; 29; 30]; % input data
indx = floor(1:2/3:numel(g)+1);          % indices with odd elements repeated
indx = indx(2:end-1);                    % remove first and last element
result = g(indx);                        % index
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
1

You can do something like this:

g =  [5; 6; 11; 12; 17; 23; 24; 29; 30];
n = size(g, 1);

f(1,:) = g(1,:);
for i = 2:(n-1)
   if mod(i,2) == 1
       f = [f; g(i,:)];
       f = [f; g(i,:)];
   else
       f = [f; g(i,:)];
   end
end
f = [f; g(n,:)];

where f is your new matrix.

Renato
  • 161
  • 7
1

You can do something like this as well.

%Creating a vector f with double the number of elements
f = zeros(2*size(g,1),1)

%Insering an element of g into the even indices of f - odd indices are still zero
f(2:2:end+1) = g;

%Inserting an element of g into the odd indices of f - even indices already have the g values
f(1:2:end) = g;

%Remove every element from the second odd index
f(3:4:end) = [];

Edit 2:
There is also another way to use this.
n = 2; %number of times an odd element has to be repeated.
f = kron(g, ones(n*1,1)); % This is the Kronecker tensor product

f(3:4:end) = [];

This will also work. 
  • Sorry! I have edited my answer to suit the question. Thanks for pointing it out. – Vijay Karthik Apr 15 '19 at 23:19
  • @CrisLuengo, I posted two solutions, yet you downvoted my answer. – Vijay Karthik Apr 16 '19 at 01:13
  • No, I changed my downvote to an upvote after you edited your answer. There are currently no downvotes on your answer. And you can see on [your reputation tab](https://stackoverflow.com/users/8028344/vijay-karthik?tab=reputation) that you don't have any downvotes at all. – Cris Luengo Apr 16 '19 at 05:14
  • Thanks for your response, the comments helped out allot. Top dog! – Danny J Stanzl Apr 18 '19 at 09:57