Good method to store pyramid shaped cell array?

조회 수: 5 (최근 30일)
Xiaohan Du
Xiaohan Du 2018년 1월 15일
편집: Xiaohan Du 2018년 1월 18일
Hi all,
I need to store some matrices in a cell, say I have 4 matrices: e1, e2, e3, e4, I need to compute the product of the matrix transpose and the matrix itself like:
e1' * e1; e1' * e2;
e2' * e2; e2' * e3;
e3' * e3; e3' * e4;
e4' * e4;
So the matrix transpose product itself, and the matrix transpose multiply the next matrix. I can use a 4-by-2 cell to store these, but the last cell element would be void, any better ways? I'd like to keep the hierarchical structure. Cheers!
Edit: I'm sorry, I should have made the question clearer. I just generate a 4-by-2 cell array and store these in it, leaving the last cell element void. I think it is fine for now.

채택된 답변

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2018년 1월 18일
You can make use the of the linear indexing of MATLAB. Basically 2D matrix can be stored as a single dimension cell array. To access a particular row,column you can convert the subscript to linear index and then access it from the array. Example code can be like below:
MAT_cell = cell(1,7);
MAT_cell{1} = "e1'*e1";
MAT_cell{2} = "e2'*e2";
MAT_cell{3} = "e3'*e3";
MAT_cell{4} = "e4'*e4";
MAT_cell{5} = "e1'*e2";
MAT_cell{6} = "e2'*e3";
MAT_cell{7} = "e3'*e4";
matSize = [4 2];
% The index (3,2) can be accessed like below:
linIndex = sub2ind(matSize,3,2);
val = MAT_cell(linIndex)
I hope this will help you.
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 1월 18일
I do not understand why you used "" string objects in combination with cell arrays? If you were trying to create an array of string objects then use MAT_cell = strings(1,7) and use () indexing instead of {} indexing.
But it looks to me as if the poster is not interested in constructing strings and instead wants actual computation.
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2018년 1월 18일
Hi Walter,
I just represented matrices as string, just for the understanding and simplicity purpose. The string should be replaced by the actual matrix in the original implementation.

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2018년 1월 18일
If you have too many void cells in an array, you could consider a sparse encoding scheme, where you store the indices and the contents of your cell structure, similar to SPARSE. In your case however, having a single void cell, I would not take the trouble. Nonetheless, here is one approach:
C = {'one',[],[] ; [] 2 [] ; [] 3 4 ; [] [] [] ; 5 '6' []} % a cell array with many voids
% encode into sparse format using a structure
tf = ~cellfun(@isempty,C) ; % non-empty cells
S.size = size(C) ;
S.index = find(tf) ; % index
S.values = C(tf) ;
whos % encoding into sparse saves some memory ...
% rebuild
C2 = cell(S.size) ;
[C2{S.index}] = deal(S.values{:})
isequal(C, C2) % check ...

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by