How to properly create a table with vectors

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

do you want a matlab table object , or you simply want to "display" a table or store like a text file
see this liitle demo below (the required functions are attached)
A = rand(5,5);
CHeaders = 'COne CTwo CThree CFour CFive';
RHeaders = 'ROne RTwo RThree RFour RFive';
MyWriteTable('TableData.dat',A,'13.4E','w',Makemat(CHeaders),Makemat(RHeaders))
next, of course , if you're after speed , don't use three nested loops.... and preallocate memory
this one too may interest you
Guido
Guido 2023년 5월 31일
@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).

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 5월 31일
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

Guido
Guido 2023년 5월 31일
편집: Guido 2023년 5월 31일
First of all thank you :) .
The ID will be a double without the hastag (but you are right I should have writen that properly, I will modify the message).
So you are saying that 'VariableTypes',['double','double','double','double','cell'] is a correct way to put it? Or using array being carefull enoug would be better for some reason?
Because I will need this table as an imput for many other programs and other people migth use it as an imput therefore I would prefere to do it in the best way possibile (but also in the least time consuming way if possible).
Also another question: is it somehow faster to define the element of the table separatly as you did (and as is usually done in many example I have seen) or is just for more clarity of the code?
Always best to share the actual data ;)
I don't know enough about your application to really answer any of those questions. What is best is an extremely subjective measure. One way to look at it is the amount of memory needed to store your table in memory.
ID = [00001;00001];
Value1 = [0;2];
Value2 = [0;55];
Value3 = [0;1];
Vector = {[00012,00402,01312,12345];[00042,00420]};
T1 = table (ID,Value1,Value2,Value3,Vector)
T1 = 2×5 table
ID Value1 Value2 Value3 Vector __ ______ ______ ______ _____________________ 1 0 0 0 {[12 402 1312 12345]} 1 2 55 1 {[ 42 420]}
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 __ ______ ______ ______ _____________________________ 1 0 0 0 12 402 1312 12345 1 2 55 1 42 420 NaN NaN
whos T1 T2
Name Size Bytes Class Attributes T1 2x5 2179 table T2 2x5 1987 table
Another metric might be how you use the table. Is it easier to work with cells that only have the data needed, or is it easier to work with vectors that are always the same size, but contain missing values?
I'm not aware of a recommendation to create a table one way or another. However, avoiding for loops and building of arrays one value at a time will speed up your code.
Guido
Guido 2023년 6월 1일
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.
You may not be able to.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2023년 5월 31일

댓글:

2023년 6월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by