Concatenate arrays of different length inside loop

18 views (last 30 days)
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

Answers (1)

Bruno Luong
Bruno Luong on 10 Aug 2022
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

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!