# How to properly create a table with vectors

1 view (last 30 days)
Guido on 31 May 2023
Commented: Cris LaPierre on 1 Jun 2023
Hi everyone, I'm trying to create a lookup table and I wanted to store a vector of double values as the last column of the table.
Those vectors will have different lenght and I was wondering the proper way to do create this table in Matlab. For example I will know the maximum possible lenght for the vectors and if necessary I could fill them with zeros so that every vector has the same lengh but it would be time consuming (and I hope not necessary).
I still miss some knowledge in table uses so if I'm making rookies mistakes or I'm using something in a not advised manner please tell me :) .
The table should be something like this:
| ID | Value1 | Value2 | Value3 (1/0) | Vector (of ID's) |
+----------+---------+----------+---------------------+-------------------------------+
| 00001 | 0 | 0 | 0 | [00012,00402,01312,12345] |
| 00001 | 2 | 55 | 1 | [00042,00420] |
| ... | ... | ... | ... | ... |
And what I was thinking for the code was something like this:
sz=rows;
i=1;
T = table ('Size',[sz 5],'VariableTypes',['double','double','double','double','cell'],'VariableNames',["ID","Value1","Value2","Value3","Vector"])
for ID = 1:MAX
for Value1 = 0:F1
for Value2 = 0:F2
%%algorithm to determine Value3 and Vector
T(i,[4])=Value3(i);
T(i,[5])=Vector(i);
i=i+1;
end
end
end
I don't really know if cell is an appropriate variable type for my purpose, and also i need to apply this to a huge database and the algorithm is pretty slow so it would be important that the table creation is as optimized as it could be.
I'm also not sure that this is the best way to do what I need, so if you have other suggestion that are not going trought a table I'm happy to hear them.
Thank you very much!
Guido.
##### 3 CommentsShow 2 older commentsHide 2 older comments
Guido on 31 May 2023
@Mathieu NOE I will need a table to use it, but it doesn't need to be a table at any cost, I just need to be able to easly access the last two column and to know that the data in there is produced with the data of the first 3 column.
I intended to preallocate memory and I actually tought that writing T = table ('Size',[sz 5], (...) ) was doing that.
For the three loops I don't know how to remove them because I need to go for every element (first loop) in every direction (second and third loop), so I immagine those will remain, but I'm always open to suggestion (but I think that for the rules here it's something we should discuss in another thread maybe).

Sign in to comment.

### Answers (1)

Cris LaPierre on 31 May 2023
First, a note that ID cannot be a double if you want to include the '#'. It must be char or string.
I think the simplest way is to add your vectors as cells.
ID = ["#00001";"#00001"];
Value1 = [0;2];
Value2 = [0;55];
Value3 = [0;1];
Vector = {["#00012","#00402","#01312","#12345"];["#00042","#00420"]};
T = table (ID,Value1,Value2,Value3,Vector)
T = 2×5 table
ID Value1 Value2 Value3 Vector ________ ______ ______ ______ ________________________________________________ "#00001" 0 0 0 {["#00012" "#00402" "#01312" "#12345"]} "#00001" 2 55 1 {["#00042" "#00420" ]}
If you are careful about it, you could also do an array, but then you need to have the same number of elements in each row of Vector.
Vector = ["#00012","#00402","#01312","#12345";"#00042","#00420",missing,missing];
T2 = table (ID,Value1,Value2,Value3,Vector)
T2 = 2×5 table
ID Value1 Value2 Value3 Vector ________ ______ ______ ______ ______________________________________________ "#00001" 0 0 0 "#00012" "#00402" "#01312" "#12345" "#00001" 2 55 1 "#00042" "#00420" <missing> <missing>
##### 4 CommentsShow 3 older commentsHide 3 older comments
Cris LaPierre on 1 Jun 2023
You may not be able to.

Sign in to comment.

### Categories

Find more on Tables in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!