tags:

views:

216

answers:

3

hi i have the following situation

      h = [0,1,1,1;
           0,0,0,0;
           1,1,1,1];

i'll check incoming values which can range between 0 and rowsize of h, i.e. in this case 2,. so my options are 0,1,2.

now i want to create a single dimensional array (let's name it j) as follows

whenever the incoming value is 0

j = [0,1,1,1]

next time if the incoming value is 1

then j = [0,1,1,1,0,0,0,0]

and so on... how is it possible to achieve this in matlab? thanks!

A: 

If the incoming value is x, you could do something like:

g = h.'
j = g(1:(x+1)*size(h,2))
Tim Goodman
I used MatLab at my old job, but don't have it installed on my current computer, so I couldn't check this for you . . . but there should at least be something close to that that works.
Tim Goodman
i did the following h=[0,1,1,1;0,0,0,0;1,1,1,1];then j=h(1:0*size(h,2));j = Empty matrix: 1-by-0
kl
i fixed the index stuff, now doing from start it seems to work, but after j = g(1:3*size(h,2)) if next incoming value is 1 my j has only 4 values...
kl
I set it up so x=1 is the first row, x=2 the first two rows . . . So j will only have 4 values for x=1, 8 values for x = 2, etc. You could replace x with (x+1) if you want 0 to mean "first row", 1 to mean "first two rows", etc.
Tim Goodman
In fact I just changed x to (x+1) in my answer . . . now I believe it should match what you want.
Tim Goodman
A: 

Hi

Matlab, as you know, indexes from 1 so you will need to add 1 to the index 0,1,2 to get the row identifier for h. So if the input is 'index'

j = h(index+1,:)

Then, for the next index

j = [j h(index+1,:)]

and so on.

Regards

Mark

High Performance Mark
Mark it seems that it takes the values one bye one, not one row at once...?
kl
You're right, I mistyped the code, I've now corrected it to take the whole row. Sorry about the confusion,
High Performance Mark
thanks! thanks!
kl
A: 

Try this (with x as your vector of incoming values):

j = reshape(h(x+1,:).',1,[]);

The above uses x+1 as an index to select copies of the rows, then transposes and reshapes the result into a single row vector. Here's a test:

>> h = [0 1 1 1; 0 0 0 0; 1 1 1 1];
>> x = [0 0 0];
>> j = reshape(h(x+1,:).',1,[])

j =

     0     1     1     1     0     1     1     1     0     1     1     1
gnovice

related questions