# Cell array Numeric data splitting

2 views (last 30 days)
Karthik Nagaraj on 19 Nov 2020
Commented: Karthik Nagaraj on 26 Nov 2020
My requirement is to create generate numerous complex matrices of 15x15, and extract the diagonal elements and upper triangle elements as column vectors, and finally combine them into a single column vector.
As a part of the implementation I have done some code which looks similar to this.
I have created a program to generate 6 complex matrices using for loop, and stores them in a cell array.
Code 1:
clear all;
close all;
clc;
N=15;
k=6;
C=cell(k,1)
for j = 1:k
C{j}= rand(N,N) + 1i*rand(N,N); % Complex Matrix
end
Now the cell C 6 x1 contains 15x15 complex double numeric data in each cell array.
For extracting the diagonal elements extract the diagonal elements and upper triangle elements as column vectors and finally combine them into a single column vector for a Matrix C I have the following code.
Code 2:
D = diag(C)
L = triu(C,1)
U = tril(C,-1)
V= [D; real(U(:)); real(L(:))].
I would like to combine this Code 2 approach for the Cell C obtained in Code1 by splitting each array in cell C- 6x1 with 15x15 in each cell into differnt arrays/ matrices of 15x15 order and then do the code2 operation.
Is it better to use the cell array approach to create a loop of matrices, or is there any other way to do? Please guide me.

I think you don't need to use "cell" for C {j}, however you can use "cell" for V {j}.
something like that:
clearvars;
clc;
N=15;
k=6;
for j = 1:k
C = rand(N,N) + 1i*rand(N,N); % Complex Matrix
D = diag(C);
L = triu(C,1);
U = tril(C,-1);
V{j}= [D; real(U(:)); real(L(:))];
end

#### 1 Comment

Karthik Nagaraj on 26 Nov 2020
Without considering cell operation,
C = rand(N,N) + 1i*rand(N,N)
will yield only one matrix

R2019b

### Community Treasure Hunt

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

Start Hunting!