Converting Numeric Matrix to Cell Array nesting columns into line (LSTM training input data)

T
texlant-trn · May 31, 2021 · 2K views
Question
Please, how can I convert a Numeric Matrix (1400 lines/steps x 30 columns/features) to Cell Array (1400x1) Nx1 as required for LSTM training?   In this case, the 30 columns by line must be nested into the one cell array each line. In outer view, cell array will be (1400x1), but when click on cell it will open as (1x30) containing the 30 features columns in one line. Simplified example: From (5x4) matrix A (lines: timesteps/observations, columns: input features data) 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8 To (5x1) cell array C (Nx1 as requested by LSTM, N = steps) nesting features columns into the cell: {1x4} => (1 2 3 4) inside line cell (1x4) {1x4} => (2 3 4 5) inside line cell (1x4) {1x4} => (3 4 5 6) inside line cell (1x4) {1x4} => (4 5 6 7) inside line cell (1x4) {1x4} => (5 6 7 8) inside line cell (1x4)   I can build it manually from an empty cell array by entering in each line cell and pasting data (30 columns) into them, but my data has 1400 lines.   The label (output) categorical vector (5x1 example) for the LSTM is ready (1400x1 real) and contains binary classification (0, 1) for each line as expected responses.
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 26, 2026
Use mat2cell(). Here is an example
 
 
A = [ ...
    1 2 3 4
    2 3 4 5
    3 4 5 6
    4 5 6 7
    5 6 7 8];

B = mat2cell(A, ones(size(A,1),1), size(A,2));


C = num2cell(A) converts array A into cell array C by placing each element of A into a separate cell in C.

The num2cell function converts an array that has any data type—even a nonnumeric type.

 

a = magic(3)
a = 3×3

     8     1     6
     3     5     7
     4     9     2

c = num2cell(a)
c=3×3 cell array
    {[8]}    {[1]}    {[6]}
    {[3]}    {[5]}    {[7]}
    {[4]}    {[9]}    {[2]}
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!