How can to convert this following cell to single matrix

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

4+3+4+3 = 14, why 15?
Repeating Per's question, do you mean 4-by-14? If not, what is supposed to be in the extra column and where does the extra column go?
for the extra column, i want to input zeros to them.
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??
as the last column mr...

Sign in to comment.

 Accepted Answer

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)

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.
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

end+1 mean that we add 1 column mr??or what?
Yes, the END statement, when used as an index, picks out the largest value of that dimension. Did you run the example and see what happens?
Look:
A = [1 2 3];
A(end+1) = 4
Matt: There are a bunch of zeros missing from your array. For example B(3,4), B(4,4) should be zero because A would look like (for the first 4 columns only)
11 12 13 14 etc.
21 22 23 24 etc.
31 32 33 00 etc.
41 42 43 00 etc.
The other sets of 4 columns should also have zeros in them where the lengths don't match up.
I am not sure what you are talking about, IA. The OP shows one cell array that would make a matrix if it had 3 extra zeros. It was my understanding that we only needed to convert the array shown, not all "pages" as it were.
For example, changing v(:,:,1) everywhere it appears in my code to v(:,:,2) does nothing for the outcome....
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.
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 Operators and Elementary Operations 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!