MATLAB Answers

Indexing concurrent array elements and incrementing

4 views (last 30 days)
Tony
Tony on 19 Aug 2020
Answered: J. Alex Lee on 20 Aug 2020
I am dealing with large binary files and I am looking for an efficent way to index concurrent elements in an array after reading the raw binary data into an linear array.
For example:
Assume I have a linear array of 'uint8', which was read from a binary file. I know that a single "record" consists of 16 bytes and that the first three bytes of each record contains a time stamp.
What is the most efficient way to index the first three elements of the array, then increment by 13 and read the next three elements and so on until the end of the array?
For single byte elements in a recod I do the following, which is quite fast:
array = rawData(start:recordSz:end);
I would like to do something similar for multi byte elements.
Thanks!

  0 Comments

Sign in to comment.

Accepted Answer

J. Alex Lee
J. Alex Lee on 20 Aug 2020
First you could use "reshape" to create a 2D array whose rows corresponds to a record, then slice up the 2D array into however many variables constitute a record, of varying byte lengths
databytestream = % ... data stream
% how each record is organized: bytes per variable in record
varByteLengths = [13,3]
% compute the number of bytes per record
recordByteLength = sum(varByteLengths)
M = rand(23,16)
% reshape into number of records by number of bytes per record
M = reshape(databytestream,[],recordByteLength)
% compute start and end indices (within a record) for each variable
idx1 = cumsum(varByteLengths)
idx0 = [1,idx1(1:end-1) + 1]
% extract each variable as a new 2D array
for i = length(varByteLengths):-1:1
var{i} = M(:,(idx0(i):idx1(i)));
end

  0 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!