Convert 4 cell arrays into 4 columns in matrix

조회 수: 3 (최근 30일)
MadjeKoe
MadjeKoe 2020년 12월 14일
편집: Adam Danz 2020년 12월 27일
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
MadjeKoe
MadjeKoe 2020년 12월 14일
편집: MadjeKoe 2020년 12월 14일
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
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 12월 14일
"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

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2020년 12월 14일
편집: Adam Danz 2020년 12월 24일
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
MadjeKoe
MadjeKoe 2020년 12월 16일
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.
Adam Danz
Adam Danz 2020년 12월 16일
편집: Adam Danz 2020년 12월 27일
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."

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by