1
votes

Im semi-new with matlab, i've been using it in my course for a while now, but never really been taken in by it.

I have a vector of quite a large size, it is a sound file to be accurate. I'm required to take every 128 elements from this vector, and add them to a matrix.

So matrix row 1 will contain the first 128 (1-128) elements, matrix row 2 will contain the second 128 (128-256) etc...

How can I go about doing this? I've looked up the matlab mathworks help files and havent been able to find anything. I know I can append matrices using z = [x,y] but its not working for me...

Appreciate any help, thanks!

4

4 Answers

1
votes

You can do this with the reshape command:

>> A = [1 2 3 4 5 6];
>> B = reshape(A, 3, 2)'
B = 
      1 2 3
      4 5 6
1
votes

Look at the reshape command. If you start with a (N*128 by 1) vector then with reshape(A,[N,128]) you should get a (N by 128) matrix.

0
votes

As others said reshape command is the right tool for you. But before you start using reshape, you would like to make sure two things:

  1. usually for any sound file there will be some header information, you need to start reading from the file position after the header information. you can find online manuals to get the size of header-data, for example the canonical sound data format can be found here: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ If the format of your sound file is something else then you'll have to find it out.

  2. the number of samples to be read should be either truncated or padded to multiple of 128 since you want a matrix of N*128 size

0
votes

This can be done slightly more conveniently even, as reshape can compute one of its argument itself

In MarkD's answer:

A = [1 2 3 4 5 6];
B = reshape(A, 3, 2)'

replace the second line with

B = reshape(A, 3, [])'

The [] input tells reshape: determine yourself what this should be (length(A)/3 in your case)