How to properly create a table with vectors
조회 수: 166 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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)
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)
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!