필터 지우기
필터 지우기

Indexing concurrent array elements and incrementing

조회 수: 5 (최근 30일)
Tony
Tony 2020년 8월 19일
답변: J. Alex Lee 2020년 8월 20일
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!

채택된 답변

J. Alex Lee
J. Alex Lee 2020년 8월 20일
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개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by