How can to convert this following cell to single matrix
Mostrar comentarios más antiguos
This question related with http://www.mathworks.com/matlabcentral/answers/47531-how-can-i-make-this-following-matrixe.
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 comentarios
per isakson
el 6 de Sept. de 2012
Editada: per isakson
el 6 de Sept. de 2012
4+3+4+3 = 14, why 15?
Matt Fig
el 6 de Sept. de 2012
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?
Febri
el 6 de Sept. de 2012
Febri
el 6 de Sept. de 2012
Matt Fig
el 6 de Sept. de 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??
Febri
el 6 de Sept. de 2012
Respuesta aceptada
Más respuestas (2)
Image Analyst
el 6 de Sept. de 2012
0 votos
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 comentarios
Febri
el 6 de Sept. de 2012
Matt Fig
el 6 de Sept. de 2012
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
Image Analyst
el 6 de Sept. de 2012
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.
Matt Fig
el 6 de Sept. de 2012
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....
Image Analyst
el 7 de Sept. de 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
el 7 de Sept. de 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...
Categorías
Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!