简体   繁体   English

分割数组并导出值,同时在matlab / octave中调整部分

[英]splitting array and export values while adjusting the sections in matlab / octave

How can I adjust this code to have it so the Output will add items at the beginning and the end of each section? 如何调整此代码以使其具有输出,以便在每个部分的开头和结尾添加项目?

Working Code example: 工作代码示例:

values = 1 : 11;
binSize = 3;
fileNum = 1;
n = numel(values);
for i = 1 : binSize : n
   part = values(i : min(n, i + binSize - 1));
   fprintf('File %d contains %s\n', fileNum, mat2str(part));
   fileNum = fileNum + 1;
end

- --

Output:

File 1 contains [1 2 3]
File 2 contains [4 5 6]
File 3 contains [7 8 9]
File 4 contains [10 11]

How can I adjust this code to have it so the Output will add items at the beginning and the end of each section? 如何调整此代码以使其具有输出,以便在每个部分的开头和结尾添加项目?

Example of output I'm looking for: 我正在寻找的输出示例:

Output I'm looking for:

File 1 contains [1 1 1 2 3 3 3]
File 2 contains [4 4 4 5 6 6 6]
File 3 contains [7 7 7 8 9 9 9]
File 4 contains [10 10 10 11 11 11]

The reason why I'm doing it this way: I have an array of audio wave files that I want to use to fade in and fade out for each section. 我这样做的原因是:我有一个音频波形文件数组,希望用于每个部分的淡入和淡出。

Edit your fprintf part with a padarray implementation that replicates data at the border elements (end and beginning) along the row, as shown here - 使用padarray实现编辑fprintf部分,该实现​​复制沿行的边框元素(结束和开始)处的数据,如下所示-

fprintf('File %d contains %s\n', fileNum, mat2str(padarray(part,[0 2],'replicate')));

Now, padarray is part of the Image Processing Toolbox, which if you don't have, you can use repmat instead, as shown next - 现在, padarray是Image Processing Toolbox的一部分,如果没有的话,可以使用repmat ,如下所示-

part_mod = [repmat(part(1),1,2) part repmat(part(end),1,2)];
fprintf('File %d contains %s\n', fileNum, mat2str(part_mod));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM