-3

I need to build an arrangement from the elements of two other arrangements. With the following code an array is generated but only with the last two values of X and Y.

    X = [0  7.313   14.632  18.238  18.308  18.383  18.394  18.395  18.395  18.395  18.395]
        Y = [10.882 18.276  21.939  22.018  22.093  22.105  22.106  22.106  22.106  22.106 22.106]

        Z = zeros(22,1) ;

        for i = 1:2:22
            for j = 2:2:22
               for m = 1:11
                   for n = 1:11

            Z(i,:) = X(:,m) ;
            Z(j,:) = Y(:,n) ;

                   end
               end
            end
        end

    output: Z = 18.395
                22.106
                18.395
                22.106
                18.395
                22.106
                ...

But I need to get the Z array as follows:

expected output: Z = X(1)
                     Y(1)
                     X(2)
                     Y(2)
                     X(3)
                     Y(3)
                     ...

with the values: Z = 0
                     10.882
                     7.313
                     18.276
                     14.632
                     21.939
                     ...

Thanks.

Yro
  • 19
  • 6
  • 1
    So what's the desired output exactly in this example? – Luis Mendo Nov 19 '19 at 22:41
  • 1
    Because your code does not do what you want it to do, you need to make it clear what the aim of the code was. "index each element of X and Y to the Z array" is not at all clear. Please [edit] your question to include the expected output. – Cris Luengo Nov 19 '19 at 23:06
  • Thanks for your comment, I edited the question including the desired output. – Yro Nov 19 '19 at 23:20

1 Answers1

2

Just concatenate X and Y vertically and then reshape into a column. The values are read in column-major order, which gives the desired result:

Z = reshape([X; Y], [], 1);
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147