Convert 4 cell arrays into 4 columns in matrix

Hi guys!
Can anybody help me with converting 4 cell arrays that have different lengths into a matrix with 4 columns? I've been searching in the forum but I cannot get it to work. Thanks in advance!

5 Comments

Please provide an example.
This is my script:
load('raw_sd_data')
%res = [target response err m' swaptrials' orientation{4} orientation{3} orientation{2} orientation{1} target_orient' color{1} color{2} color{3} color{4} target_color' d{1}' d{2}' d{3}' d{4}' ppt trialnum ];
targets = res(:,1);
orientation_gabor = res(:,[9 8 7 6]);
%% Matching the targets to its orientations
tar = [1,2,3,4];
orientation_target = [];
for T = 1 : 4
select_tar = targets==tar(T);
orientation_target{T} = orientation_gabor(select_tar, T);
end
matrix_orientation_targets = cell2mat(orientation_target)
I want to put the cell arrays I created with the for loop into a matrix, but cell2mat doesn't work since the arrays have different lengths.
Discl: I have read the original question only, I have not checked the code, as commented.
Are you looking for this one? If yes, you can't, or you have to fill the remaining elements with NaN and make it works.
Yes, I am! Do you maybe know how I can fill in the blanks with NaN?
I'm trying to make the following piece of script work, which is why I wanted to put the cell arrays into a matrix (because you cannot subtract cell arrays from eachother). Do you happen to know an alternative way to make it work?
%% Relative orientations
influencer = [1 1 1 2 2 2 3 3 3 4 4 4];
target = [2 3 4 1 3 4 1 2 4 1 2 3];
relative_orientation= cell(12,1);
for i=1:12
relative_orientation{i} = orientation_target(influencer(i)) - orientation_target(target(i));
end
"Yes, I am! Do you maybe know how I can fill in the blanks with NaN?"
Here
https://www.mathworks.com/matlabcentral/answers/267216-how-to-make-each-vector-of-equal-size-in-a-cell-by-adding-nan#answer_209066

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 14 Dec 2020
Edited: Adam Danz on 24 Dec 2020
This answer shows how to combine column vectors of different lengths into a matrix with NaN values as fillers.
Applied to your data, that would look like,
maxNumRow = max(cellfun(@(c) numel(c), orientation_target)); % max length
mPad = cell2mat(cellfun(@(c){padarray(c,[maxNumRow-numel(c),0],NaN,'Post')},orientation_target));
Sample of the result
mPad(1:10,:)
ans =
28 -87 -44 42
-15 -79 44 8
-70 -3 -18 30
50 -1 -41 75
-38 -87 32 -67
-82 -57 12 65
78 24 -24 -90
-63 66 -6 54
46 45 86 13
-1 0 83 -46

6 Comments

Thank you very much. 1 last thing, I seem to get 1 large vectors with 4 of the cell arrays in it instead of 4 different vectors, do you maybe know how to fix this?
When I apply it to your data from ras_sd_data.mat using the code you provided, the results are as they appear in my answer,
targets = res(:,1);
orientation_gabor = res(:,[9 8 7 6]);
%% Matching the targets to its orientations
tar = [1,2,3,4];
orientation_target = [];
for T = 1 : 4
select_tar = targets==tar(T);
orientation_target{T} = orientation_gabor(select_tar, T);
end
% Add these two lines
maxNumRow = max(cellfun(@(c) numel(c), orientation_target)); % max length
mPad = cell2mat(cellfun(@(c){padarray(c,[maxNumRow-numel(c),0],NaN,'Post')},orientation_target));
This is what orientation_target looks like
orientation_target =
1×4 cell array
Columns 1 through 3
{4136×1 double} {4144×1 double} {4097×1 double}
Column 4
{4081×1 double}
Thank you, I accidentely changed something in the script above which is why it didn't work. It works now, but I still got a little problem. The rows in the matrix do not correspond with the right trial anymore, and after thinking about it I don't think that there is a solution for this, can you agree? Or do you think that it's possible to put the NanN's in the correct rows?
This solution pads the shorter vectors and the end. The NaN values can be inserted anywhere but you need to know which trials are missing for each column of data.
If you know which trials are missing, it's not a problem.
If you don't know which trials are missing and can't figure it out somehow, there's no solution.
So, do you have the trial numbers that are present or that are missing?
Yes, I know which ones are missing:
When targets = 1, I want to select orientation_target{1}, and make the others NaN. This is the same for the rest: if targets = 2, select orientation_target{2} etc.
orientation_target{1} is a column vector. I don't understand "When targets = 1, I want to select orientation_target{1}, and make the others NaN."

Sign in to comment.

Categories

Asked:

on 14 Dec 2020

Edited:

on 27 Dec 2020

Community Treasure Hunt

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

Start Hunting!