How to properly create a table with vectors

166 views (last 30 days)
Guido
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 Comments
Guido
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
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 Comments
Guido
Guido on 1 Jun 2023
So what I have is a list of voxel and for each of them I have to see in every direction what other voxel they would encounter (sort of raytracing but without reflection or other effect like that). So basically the first column will have the conisdered voxel ID (that will change after having considered all directions), in the second and third column I have the direction of the "ray", in the 4th column I will just store the information "does this ray intercept voxel of a specific type before arriving to the horizon?" and the 5th column need to be a list of all the voxel encountered in the path of the "ray".
How could I avoid for loops if I need to say "for every voxel i look in all direction that depends from n and m" where i>>0 and n and m could have around 40 different values and I need to combine all of them for every voxel.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!