Vector slicing for loop

조회 수: 103 (최근 30일)
P Walter
P Walter 2020년 5월 17일
답변: Ang Feng 2020년 5월 17일
I'm very new to Matlab and stuck right now with a for loop. I'd like to slice the array and grab blocks of 512 Samples. I want to grab the 10 blocks and write them into a new array. My following Code only grabs the first block.
for i=1:10;
blockindex=0;
startpoint = ((blockindex -1)*512)+1;
endpoint = ((blockindex) * 512)+1;
helparray = sound6_(startpoint:1:endpoint);
mic1 = [mic1; + helparray];
blockindex = blockindex + 40;
end
plot(mic1);

답변 (1개)

Ang Feng
Ang Feng 2020년 5월 17일
I am not 100% sure about what you want to do exactly by 'slice'. If you want to rearrange the signal to a specific format, then the reshape function helps you do that. See the link to reshape:
For a signal stored as array A, and you can just reshape A by the dimension you specify
n = floor(numel(A)/512);
B = reshape(A,[512 n]);
The you have columns of B is a vector of 512 elements, and save the first 10 columns to a new variable is easy.
C = B(:,1:10);
numel is the function to find the number of elements of a matrix.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by