How can to convert this following cell to single matrix

10 views (last 30 days)
How can to convert this following cell to single matrix result(4,15)?
val(:,:,2) =
[2x4 double] [2x3 double] [2x4 double] [2x3 double]
[2x3 double] [2x4 double] [2x4 double] [2x4 double]
I have problem to use cell2mat, because amount the first row and second row is different. anyone have suggestion??
  6 Comments
Matt Fig
Matt Fig on 6 Sep 2012
O.k., so now we know what is in the extra column, but where does it go? As the last column, as the first column, as a column in the middle somewhere??

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 7 Sep 2012
A = cell(size(val));
q1 = [val(:),arrayfun(@(x)zeros(2,x),max(c(:)) - c(:),'un',0)];
A(:) = num2cell(q1,2);
out = cell2mat(cellfun(@(x)[x{:}],A,'un',0));

More Answers (2)

Image Analyst
Image Analyst on 6 Sep 2012
What do you want to put there, where there are mismatches? NaNs? Zeros? Something else. I think you need to provide an example of what you have and what you'd like to end up with.

Matt Fig
Matt Fig on 6 Sep 2012
Edited: Matt Fig on 6 Sep 2012
O.k., here is an example. First I will build a cell array as you show, then I will get it to matrix B.
% First build your cell array, at least what you show of it.
R = rand(2,4);
X = rand(2,3);
val(:,:,1) = {R,X,R,X;X,R,R,R};
% Now that we have val, convert it to a matrix. 4-by-15.
B = val(:,:,1); % Just because I don't know much else about val.
B(:,end+1) = {zeros(2,1),[]}; % pad
B = cell2mat(B)
  6 Comments
Image Analyst
Image Analyst on 7 Sep 2012
I could be wrong, but he's saying that the first cell column has 4 number columns for the first cell column and only 3 for the second pair of rows. Let's say, for the sake of simplicity, that all the values were 1. So if you were to write the cells out out you'd get this:
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
so see how the first cell (upper left set of 8 1's) is 4 wide but the set below it is only 3 wide? and for the next group over, the upper set is only 3 wide but the group below it is 4 wide. So I asked what to put in the missing spots, in other words, what should be put where the ? are in the matrix below, so that it's a rectangular matrix:
1 1 1 1 1 1 1 ? 1 1 1 1 1 1 1 ?
1 1 1 1 1 1 1 ? 1 1 1 1 1 1 1 ?
1 1 1 ? 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 ? 1 1 1 1 1 1 1 1 1 1 1 1
He said he wants zeros where those ? are. So I think he or she wants:
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0
1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
or (getting rid of spaces):
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0
1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
I don't see the zeros in those places in your solution. At least that's how I interpreted his question. Again, I could have interpreted it incorrectly. Maybe Febri will clarify which way it's wanted.
Matt Fig
Matt Fig on 7 Sep 2012
I see now what you mean. Your result is 4-by-16, but I was going by the need for 4-by-15 as specified. So starting with:
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
We squeeze things together left as far as we can, and pad:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
1 1 1 <--1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 <--1 1 1 1 1 1 1 1 1 1 1 1
To get:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
If it wasn't for the the stated 4-by-15 result I might have interpreted the question the same way you did. I take it the OP knows what he wants, but not how to get there, but I admit the problem is ambiguous. This is one reason I always ask people to post sample inputs and expected outputs and not just a description! If your example above was the sample input, then the expected output would really (and immediately) show who interpreted the question the way the OP wants...

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!