Create a matrix that has a vector in each cell
Show older comments
I need to create a matrix that has a vector in each cell and I need to save it as a .dat file in order to be opened by another MATLB script and easily be used into concatenated for cycles. Therefore I need a matrix that can be easily indexed.
7 Comments
"...and I need to save it as a .dat file in order to be opened by another MATLB script..."
Is there a particular reason why you cannot simply pass the array, e.g. as an input/output argument?
That would most likely be simpler and more efficient than exporting to and then importing from file.
Emilio Pulli
on 27 Dec 2021
Stephen23
on 27 Dec 2021
"Yes there is a reson but the crucial point is how to use and create 3d matrix"
You do not mention anything about 3D arrays in your question, only nested vectors in a (cell?) array. Thus your comment does not seem to be related at all to your question.
"Therefore I need a matrix that can be easily indexed."
All arrays are "easily indexed", even 3D ones.
So far the actual task and question are unclear.
Emilio Pulli
on 27 Dec 2021
Stephen23
on 27 Dec 2021
"...I need to save it as a .dat file"
AFAIK there is no universal standard that defines .DAT files, although they commonly seem to be text files.
Please provide a reference for the particular .DAT file format that you wish to save data in, and we will see if it is possible to save container arrays with nested arrays.
Emilio Pulli
on 27 Dec 2021
Walter Roberson
on 27 Dec 2021
importdata() cannot handle 3D matrices.
Answers (1)
I need to create a matrix that has a vector in each cell
Example:
M = 3; N = 4; L = 5;
A = cell(M, N);
V = zeros(1,L);
for n = 1 : N
for m = 1 : M
V = V + randn(1,L); %in other words, it can be something computed
A{m, n} = V;
end
end
A
This is a 2D matrix with a vector in each cell.
A simple .dat file that can be imported by MATLAB with the function importdata, containing only numbers
That is a problem. You do not have a numeric matrix, you have a Cell Array.
As outsiders, we have to presume that the vectors can be of different length for each location -- if they were all the same location you would have used a 3D array instead of a matrix with a vector at each cell.
It is possible to write something like that out to a text file, but it would take a little bit of work to load it. Something like:
- Open a file for writing
- write out the number of rows and number of columns on the first output line
- proceed through the cell array in linear order, all of the first row, all of the second row, and so on. For each cell, write out the elements of the vector on a line by themselves. This obeys the rule of "containing only numbers" while allowing different cells to be different length; the length of each cell entry is implied by the number of entries on the line.
- close the file
If you are happen to be able to use a 3D numeric matrix instead of a matrix with a vector at each cell, if the vector can be along the third dimension because they are all equal size, then I would suggest
- writematrix() number of rows and number of columns and length of vector to a file (creating the file)
- reshape() the matrix to 2D, like AR = reshape(A, [], size(A,3))
- writematrix() the reshaped matrix, using append mode
To restore: readmatrix() just the first line to get the original dimension. Then readmatrix() the 2nd line onward. reshape() the result to the dimensions read from the first line.
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!