WeChat Screenshot_20190422191521.png
I've got a 35*10 cell array, which contains multiple matrices with the same size(256*256). I want to convert it into a 4-D array(256*256*10*35).

댓글 수: 1

Stephen23
Stephen23 2019년 4월 23일
Important note to future readers: the accepted answer mixes up the data!

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

 채택된 답변

Matt J
Matt J 2019년 4월 22일
편집: Matt J 2019년 4월 23일

0 개 추천

Where C is your cell array:
M = reshape( cat(3,C{:}) , [256,256,10,35])
EDIT:
Or M = reshape( cat(3,C{:}) , [256,256,35,10]) ?

댓글 수: 4

Stephen23
Stephen23 2019년 4월 22일
편집: Stephen23 2019년 4월 22일
Note that this will not put the cell array 1st dimension along the numeric array third dimension, or the 2nd along the fourth, it will simply follow linear-indexing order, which will result in those two dimensions being mixed up in the final array. A permute or a transpose is required.
Matt J
Matt J 2019년 4월 22일
편집: Matt J 2019년 4월 22일
Note that this will not put the cell array 1st dimension along the numeric array third dimension, or the 2nd along the fourth
I think it will -- because of the way cat is applied.
Stephen23
Stephen23 2019년 4월 23일
편집: Stephen23 2019년 4월 23일
"I think it will -- because of the way cat is applied."
Nope, that is incorrect (for the reasons that I already explained in my previous comment). You could concatenate along any dimension >2 and it makes absolutely no difference to the linear-indexing order that your answer uses: this order will always mix up the data. Your answer is flawed without a transpose or a permute (simplest would be a transpose beforehand).
It very easy to demonstrate with a simpler example, which should also make the reason clear. Here I used a 3x5 cell array of 1x1 numeric arrays:
>> C = {11,12,13,14,15;21,22,23,24,25;31,32,33,34,45} % digits= row col
C = 11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
>> M = reshape(cat(3,C{:}),[1,1,5,3])
M =
ans(:,:,1,1) = 11
ans(:,:,2,1) = 21
ans(:,:,3,1) = 31
ans(:,:,4,1) = 12
ans(:,:,5,1) = 22
ans(:,:,1,2) = 32
ans(:,:,2,2) = 13
ans(:,:,3,2) = 23
ans(:,:,4,2) = 33
ans(:,:,5,2) = 14
ans(:,:,1,3) = 24
ans(:,:,2,3) = 34
ans(:,:,3,3) = 15
ans(:,:,4,3) = 25
ans(:,:,5,3) = 45
Here we can see for example, that along the 3rd dimesion M(1,1,:,1) (which has size 5) contains the values 11, 21, 31, 12, and 22. Your code has mixed up the elements of those two dimensions, exactly as I said it would: three elements from the first column, two elements from the second column! Lets look along the third dimension:
M(1,1,:,1) = 11,21,31,12,22
M(1,1,:,2) = 32,13,23,33,14
M(1,1,:,3) = 24,34,15,25,45
Try it on any non-square example and you will find that your code always mixes up the data.
Compare to my (correct) answer:
>> M = cell2mat(permute(C,[4,3,2,1]))
M =
ans(:,:,1,1) = 11
ans(:,:,2,1) = 12
ans(:,:,3,1) = 13
ans(:,:,4,1) = 14
ans(:,:,5,1) = 15
ans(:,:,1,2) = 21
ans(:,:,2,2) = 22
ans(:,:,3,2) = 23
ans(:,:,4,2) = 24
ans(:,:,5,2) = 25
ans(:,:,1,3) = 31
ans(:,:,2,3) = 32
ans(:,:,3,3) = 33
ans(:,:,4,3) = 34
ans(:,:,5,3) = 45
Along the 3rd dimension M(1,1,:,1) (which has size 5) are the elements 11, 12, 13, 14, 15, thus keeping exactly the sequence of the orginal cell array's 2nd dimension (which has size 5), even though its dimensions have been swapped. Lets look at all of the 3rd dimension:
M(1,1,:,1) = 11,12,13,14,15
M(1,1,:,2) = 21,22,23,24,25
M(1,1,:,3) = 31,32,33,34,35
The 1st dimension of C has been correctly converted to the 3rd dimension of M.
In case my simple example is too simple for you, here is the complete 35x10 cell array:
>> C = arrayfun(@(n)rand(256,256),nan(35,10),'uni',0);
>> M = reshape(cat(3,C{:}), [256,256,10,35]); % your buggy answer
>> isequal(M(:,:,10,1),C{1,10}) % your buggy answer
ans = 0
>> M = cell2mat(permute(C,[4,3,2,1])); % my correct answer
>> isequal(M(:,:,10,1),C{1,10}) % my correct answer
ans = 1
The onus of proof lay with me: for that I have provided two examples of why your answer is buggy, I have also explained why it is buggy, and also explained how it can be fixed.
Matt J
Matt J 2019년 4월 23일
편집: Matt J 2019년 4월 23일
OK, I think you should allow for the possibility, though, that the OP had a typo and really meant to say that the final dimensions should be [256,256,35,10], since that requires the least data reorganization. In that case, my solution is valid with the following small change.
M = reshape( cat(3,C{:}) , [256,256,35,10])
It is also possible that the OP meant a 3rd alternative,
C=C.';
M = reshape( cat(3,C{:}) , [256,256,10,35])

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

추가 답변 (1개)

Stephen23
Stephen23 2019년 4월 22일

1 개 추천

Where C is your cell array:
M = cell2mat(permute(C,[4,3,2,1]))

댓글 수: 8

Wenyi Xiao
Wenyi Xiao 2019년 4월 22일
Thank you for your help!
Stephen23
Stephen23 2019년 4월 23일
편집: Stephen23 2019년 4월 23일
@Wenyi Xiao: my pleasure.
You should know that the answer you accepted gives the data in an mixed up order.
debojit sharma
debojit sharma 2023년 6월 16일
@Stephen23 Sir, In this : M = cell2mat(permute(C,[4,3,2,1]))
what is [4,3,2,1]? Please kindly explain what will be these values if I have a cell array of size 1* 135
Stephen23
Stephen23 2023년 6월 16일
편집: Stephen23 2023년 6월 16일
Because your cell array is "one dimensional" you do not need PERMUTE, you can simply use a comma-separated list. Lets first create some fake data:
format compact
xTrain = {rand(2,2,4),rand(2,2,4),rand(2,2,4)}
xTrain = 1×3 cell array {2×2×4 double} {2×2×4 double} {2×2×4 double}
A = cat(4,xTrain{:}) % this is all you need
A =
A(:,:,1,1) = 0.2834 0.6106 0.8151 0.8142 A(:,:,2,1) = 0.2350 0.8999 0.0795 0.2219 A(:,:,3,1) = 0.9345 0.9923 0.2742 0.2406 A(:,:,4,1) = 0.2141 0.4339 0.0173 0.2571 A(:,:,1,2) = 0.5167 0.6388 0.9098 0.3225 A(:,:,2,2) = 0.0708 0.4039 0.6876 0.5883 A(:,:,3,2) = 0.6054 0.4192 0.0597 0.5061 A(:,:,4,2) = 0.1943 0.4933 0.4184 0.2042 A(:,:,1,3) = 0.2557 0.5996 0.3474 0.3618 A(:,:,2,3) = 0.6575 0.2345 0.3212 0.0317 A(:,:,3,3) = 0.7977 0.5544 0.7107 0.9662 A(:,:,4,3) = 0.4135 0.4872 0.2009 0.4120
Alternatively you could use RESHAPE and CELL2MAT (complex and slower):
A = cell2mat(reshape(xTrain,1,1,1,[]))
A =
A(:,:,1,1) = 0.2834 0.6106 0.8151 0.8142 A(:,:,2,1) = 0.2350 0.8999 0.0795 0.2219 A(:,:,3,1) = 0.9345 0.9923 0.2742 0.2406 A(:,:,4,1) = 0.2141 0.4339 0.0173 0.2571 A(:,:,1,2) = 0.5167 0.6388 0.9098 0.3225 A(:,:,2,2) = 0.0708 0.4039 0.6876 0.5883 A(:,:,3,2) = 0.6054 0.4192 0.0597 0.5061 A(:,:,4,2) = 0.1943 0.4933 0.4184 0.2042 A(:,:,1,3) = 0.2557 0.5996 0.3474 0.3618 A(:,:,2,3) = 0.6575 0.2345 0.3212 0.0317 A(:,:,3,3) = 0.7977 0.5544 0.7107 0.9662 A(:,:,4,3) = 0.4135 0.4872 0.2009 0.4120
The original answer requires permute because the cell array is a matrix, and then OP wanted to permute the cell array dimensions 1&2 to become dimensions 4&3 of the output array respectively. Your case is simpler: you only want to permute cell array dimension 2 to dimension 4. This can be achieved using the code I gave above.
But if you really want to use PERMUTE here it is:
% vvvvv the order of these is not important
% v move cell array 2nd dim to 4th dim
A = cell2mat(permute(xTrain,[1,3,4,2]))
A =
A(:,:,1,1) = 0.2834 0.6106 0.8151 0.8142 A(:,:,2,1) = 0.2350 0.8999 0.0795 0.2219 A(:,:,3,1) = 0.9345 0.9923 0.2742 0.2406 A(:,:,4,1) = 0.2141 0.4339 0.0173 0.2571 A(:,:,1,2) = 0.5167 0.6388 0.9098 0.3225 A(:,:,2,2) = 0.0708 0.4039 0.6876 0.5883 A(:,:,3,2) = 0.6054 0.4192 0.0597 0.5061 A(:,:,4,2) = 0.1943 0.4933 0.4184 0.2042 A(:,:,1,3) = 0.2557 0.5996 0.3474 0.3618 A(:,:,2,3) = 0.6575 0.2345 0.3212 0.0317 A(:,:,3,3) = 0.7977 0.5544 0.7107 0.9662 A(:,:,4,3) = 0.4135 0.4872 0.2009 0.4120
debojit sharma
debojit sharma 2023년 6월 16일
Thank you @Stephen23 sir
debojit sharma
debojit sharma 2023년 6월 16일
I am trying to implement the code given in the following link with my own dataset:
I am getting the following errors in the Train model part:
I can make out that there is some issue happening with the custom functions ('modelLoss', 'elboLoss') given in this link. But I am unable to understand how can I resolve these issues. Please kindly guide me @Stephen23 sir. I will be thankful to you.
Stephen23
Stephen23 2023년 6월 17일
@debojit sharma: ask a new question. Give sufficient information that we can replicate the error.
debojit sharma
debojit sharma 2023년 6월 17일
@Stephen23 sir, I am trying to implement the code to train VAE for image generation given in the following link using by own dataset of RGB images of size 200*200.
I am getting the following errors in the Train model part:
The code of VAE in the above link is using MNIST dataset images as input to encoder of VAE and it is being said that the decoder of VAE will output an image of size 28-by-28-by-1. But I am trying to generate RGB image of size 200*200 by training this VAE model given in the link. So, my input image is a RGB image of size 200*200. I am getting the above mentioned error in the train model part. I am not able to resolve these errors. So, please kindly guide me @Stephen23 sir regarding what changes I will have to make in this code so that I can train these VAE model to generate RGB image of size 200*200. I will be thankful to you.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 4월 22일

댓글:

2023년 6월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by