Concatenate arrays of different length inside loop

17 次查看(过去 30 天)
I'm using a loop over study participants to create an array of all nonzero values in each participant's file and want to then concatenate these arrays into one matrix/table/etc.
The problem is that there are different amounts of nonzero values in each file and therefore I cannot combine the arrays. I found the PADCAT function but don't think I can use it here because it only works on arrays, i.e. I cannot use that in the loop to append each new column to the existing matrix. The only solution I have is to use the function after the loop, but for that I need to save every array which I want to avoid (especially because I don't want to create and name variables dynamically in the loop and I didn't find a better solution). Any tip would be much appreciated!0 - Either on how to concatenate the arrays in the first place or on how to use PADCAT in my case.
https://de.mathworks.com/matlabcentral/fileexchange/22909-padcat
for i = 1:length(image_files)
header = spm_vol(image_files{i});
image_data = spm_read_vols(header); % image_data is a 350x400x12 double
vox_count = nonzeros(image_data); % create array of nonzero values for each participant
vox_count_table(:,i) = vox_count; % put values into table - one column for each participant
end

回答(1 个)

Bruno Luong
Bruno Luong 2022-8-10
What's wrong with cell?
vox_count_cell = cell(1, length(image_files));
for i = 1:length(image_files)
header = spm_vol(image_files{i});
image_data = spm_read_vols(header); % image_data is a 350x400x12 double
vox_count = nonzeros(image_data); % create array of nonzero values for each participant
vox_count_cell{i} = vox_count;
end

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by