How to simplify this code without having multilevel indexing error???

조회 수: 1 (최근 30일)
hana razak
hana razak 2017년 10월 5일
댓글: hana razak 2017년 10월 11일
Hi,
I've a matData consists of (1x96 struct) and each structure, ji has (1x35 cell) and for each cell has (20x2 double). I want to arrange the data into (1400x96 double).
As for now, I only managed to get (1400x1 double) separately for each matData by using the code below,
a1 = matData(1).ji;
aa1 = cell2mat (a1);
p1 = reshape(aa1, 1400, 1);
a2 = matData(2).ji;
aa2 = cell2mat (a2);
p2 = reshape(aa2, 1400, 1);
a3 = matData(3).ji;
aa3 = cell2mat (a3);
p3 = reshape(aa3, 1400, 1);
What should I do to simplify this code without having manually typing for all 96 matData?
I can't straightly write as,
aa = matData(1:35).ji{1:35};
because I keep getting this error 'Field reference for multiple structure elements that is followed by more reference blocks is an error'.
Thank you

채택된 답변

Cedric
Cedric 2017년 10월 5일
편집: Cedric 2017년 10월 5일
Here is one way. I build a simple test data set with 4 structs, each ji field contains 3 cells, and each cell contains a 2x2 numeric array:
S(1).ji = {[10 12;11 13],[14 16;15 17],[18 20;19 21]} ;
for k = 2:4, S(k).ji = cellfun( @(x)k*x, S(1).ji, 'UniformOutput', false ) ; end
To aggregate:
>> bigArray = cell2mat( cellfun( @(x)x(:), vertcat( S.ji ).', 'UniformOutput', false ))
bigArray =
10 20 30 40
11 22 33 44
12 24 36 48
13 26 39 52
14 28 42 56
15 30 45 60
16 32 48 64
17 34 51 68
18 36 54 72
19 38 57 76
20 40 60 80
21 42 63 84
Evaluate the internal expressions to understand, and ask if anything is unclear. Looking at your code, the only thing that you may not be familiar with is S.ji (with no struct index), which is a comma separated list (CSL), that we concatenate with VERTCAT. Then all the rest is about manipulating cell and numeric arrays.
  댓글 수: 14
Cedric
Cedric 2017년 10월 11일
편집: Cedric 2017년 10월 11일
If your data is not too confidential, the simplest way to go would be to drop me an email using my personal email (you get it when I send you notices that I posted a comment to your thread), attaching a MAT-File with matData.
If you cannot do this, the whole point of the test that I suggested was to identify a situation where there is a size mismatch.
See, if you have
>> A = {randi(10,2,3),randi(10,2,3);randi(10,2,3),randi(10,2,2)}
A =
2×2 cell array
{2×3 double} {2×3 double}
{2×3 double} {2×2 double}
where there is a size mismatch (A{2,2} is 2x2 and not 2x3 like the others), you will be able to concatenate all contents horizontally but not vertically:
>> horzcat( A{:} )
ans =
7 7 10 3 3 6 4 5 8 5 7
10 7 6 1 9 5 10 7 7 8 7
>> vertcat( A{:} )
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
The horizontal concatenation works technically, but it doesn't mean that it is really the operation that you intended to do. It certainly let the size mismatch undetected.
My guess was that you ran my code (that uses a vertical concatenation), got this type of error, and then tried using a horizontal concatenation. And while it went through, the size doesn't match your expectation.
Your comment seems to indicate that you found the size discrepancy. Now all you have to do is to understand whether it should be concatenated with the rest (maybe it is just invalid), and in what direction.
hana razak
hana razak 2017년 10월 11일
Yes, my data is not too confidential. You can send the notice in order for me to attach the MAT-File. Thank you very much. This would be a great help for me.

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

추가 답변 (1개)

KL
KL 2017년 10월 5일
s2c = struct2cell(matData);
c2c = cellfun(@(x) x',reshape(s2c,[],1,1),'uni',0);
c2a = cell2mat(cellfun(@cell2mat,c2c,'uni',0));
  댓글 수: 2
hana razak
hana razak 2017년 10월 6일
편집: hana razak 2017년 10월 7일
It only got half way through. I've got an error for this code;
c2a = cell2mat(cellfun(@cell2mat,c2c,'uni',0));
It just stated as below,
'Error in test2 (line 19) c2a = cell2mat(cellfun(@cell2mat,c2c,'uni',0));'
I don't have any idea how to correct it. So, is there any other way to get it right? Thanks a lot
hana razak
hana razak 2017년 10월 11일
편집: hana razak 2017년 10월 11일
I got it..
The codes are work very well. The error was due to bulk data.
Thank you so much. It was really very helpful!

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by