How to extract every other value from a 50000x1 array?

4 visualizaciones (últimos 30 días)
David
David el 19 de Dic. de 2014
Comentada: Stephen23 el 19 de Dic. de 2014
Hello.
I have a problem I can't solve. I read in values over a serial COM port, the valuea are 12bit ADC results but I need to send them as:
value = ADC_reading;
Serial.write(value >> 8);
Serial.write(value & 0xFF);
So when received in matlab its a stream of value pairs, for example:
2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251... though I store them as a n x 1 vector.
How can I process this vector?
I have been on the path of reading only one pair of values one byte at a time in some loop but that does not seem to be a good idea at all, I get problem when trying to solve the end value vector indexing.If there is no solution to this I 'could' send the values in its hole form but that will take at least 5 bytes per value and I am going out of my way to squeezing every little efficiency out of the setup in order to achieve as high a data throughput as possible.
Regards

Respuesta aceptada

Chad Greene
Chad Greene el 19 de Dic. de 2014
If I'm understanding your question correctly,
x = [2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251 2 251];
a = x(1:2:end)
b = x(2:2:end)
a =
2 2 2 2 2 2 2 2 2 2 2 2
b =
251 251 251 251 251 251 251 251 251 251 251 251
  3 comentarios
Chad Greene
Chad Greene el 19 de Dic. de 2014
...that happens to all of us. :)
Stephen23
Stephen23 el 19 de Dic. de 2014
You mention efficiency of data processing: if the data is large, it may help to keep the data in the original array, and just reshape it:
>> x = [2 251 2 251 2 251 2 251 2 251 2 251 2 251];
>> reshape(x,2,[])
ans =
2 2 2 2 2 2 2
251 251 251 251 251 251 251
Of course this also depends on what processing follows on from this step...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by