How to transpose a cell array blockwise?

4 vues (au cours des 30 derniers jours)
David Mrozek
David Mrozek le 3 Mar 2021
Modifié(e) : James Tursa le 4 Mar 2021
I have created a dataset D with one column and 3 rows which includes the following elements:
D1 = {1, 1} , 'Text1' with a total repetition of 15 times (1 row and 15 columns)
D2 = {2, 1} , Includes 15 300x3 double elements (1 row and 15 columns) named data1
D3 = {3, 1} , 'Text2' with a total repetition of 15 times (1 row and 15 columns)
Therefore I have the following cell array if i open D1, D2, and D3:
Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1 Text1
data1 data1 data1 data1 data1 data1 data1 data1 data1 data1 data1 data1 data1 data1 data1
Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2 Text2
When using the transpose option I would like to get the following order:
Text1
data1
Text2
Text1
data1
Text2
Text1
data1
Text2
etc.
However I am not sure how to write the code for it properly. Is there a matlba function to create "block elements" for transposing the above mentioned example?
Stay safe and healthy
David
  5 commentaires
Bjorn Gustavsson
Bjorn Gustavsson le 3 Mar 2021
This sounds like a rubbish structure for your data (I might be wrong, and I know this sounds harsh.) The structure you chose for your data should make the work simple, here you seem to combine the data in a way that makes further processing difficult.
I suggest you take a think about how you should structure the data in a way that makes it easy to process.
David Mrozek
David Mrozek le 3 Mar 2021
Modifié(e) : David Mrozek le 3 Mar 2021
I am, to this kind of desired data order, not responsible.
I need the data in this order to process it further for the CATIA V5 R21 Spline-Generator which converts such data structures into geometrical splines.
Since CATIA V5 offered a macro-based option for the Spline-Generator which I have not written myself, I am unable the meddle with the programming architecture of CATIA.
Hence I decided to write a matlab file which is prepared in the right order.
Therefore I require this this particular data order.
The first text "StartCurve" itself starts the macro, as I noticed, and generates the geometrical structure from the data beneath the first text.
The 2nd text "EndCurve" beneath the data stops the Spline-Generator. This leads me to the conclusion that the macro Spline-Generator in CATIA work row-based and not column-based.
This results in the following order:
Text1
data1
Text2
Text1
data1
Text2
I hope that this explains the background behind data structure a little bit better.

Connectez-vous pour commenter.

Réponse acceptée

James Tursa
James Tursa le 3 Mar 2021
E.g., brute force approach
result = reshape([D{1};D{2};D{3}],[],1);
  4 commentaires
James Tursa
James Tursa le 4 Mar 2021
Modifié(e) : James Tursa le 4 Mar 2021
There are multiple things going on in that one line of code.
First you specified that D{1}, D{2}, and D{3} each contained 1x15 cell arrays, so this expression stacks them vertically into a single 3x15 matrix:
[D{1};D{2};D{3}] <-- using semi-colons causes the stacking vertically (using commas would be horizontal)
The elements of the resulting 3x15 combined cell matrix, which is a regular cell array and not a nested cell array because we dereferenced the first level with the { } notation, are simply:
D{1}(1) , D{1}(2) , ... , D{1}(15)
D{2}(1) , D{2}(2) , ... , D{2}(15)
D{3}(1) , D{3}(2) , ... , D{3}(15)
Since all MATLAB matrices are stored in memory in column major order, that means the first column is first in memory, followed by the second column, followed by the third column, etc.
The reshape( ) function simply changes the dimensions of the matrix without changing the ordering of the elements in memory, so reshaping to a column vector simply produces this result:
D{1}(1)
D{2}(1)
D{3}(1)
D{1}(2)
D{2}(2)
D{3}(2)
:
D{1}(15)
D{2}(15)
D{3}(15)
Bottom line is that the concatenation and reshaping or transposing works the same way on cell arrays as it does on regular numeric or logical matrices.
David Mrozek
David Mrozek le 4 Mar 2021
Thank you very much for this detailed explanation.

Connectez-vous pour commenter.

Plus de réponses (1)

Bjorn Gustavsson
Bjorn Gustavsson le 3 Mar 2021
If I understand you right this should do what you want:
QWE = {'T1_1','T1_2','T1_3';randn(1),randn(2),randn(3);'T2_1','T2_2','T2_3'};
%
%QWE =
%
% 3×3 cell array
%
% 'T1_1' 'T1_2' 'T1_3'
% [0.5377] [2×2 double] [3×3 double]
% 'T2_1' 'T2_2' 'T2_3'
%
QWE(:)
%
%
%
% 9×1 cell array
%
% 'T1_1'
% [ 0.5377]
% 'T2_1'
% 'T1_2'
% [2×2 double]
% 'T2_2'
% 'T1_3'
% [3×3 double]
% 'T2_3'
This should extend well.
HTH
  1 commentaire
David Mrozek
David Mrozek le 3 Mar 2021
Modifié(e) : David Mrozek le 3 Mar 2021
It works if the array is unnested. However, I have some troubles to apply your code for a nested cell array.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by