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
Mathieu NOE
2023년 5월 31일
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
Mathieu NOE
2023년 5월 31일
this one too may interest you
Guido
2023년 5월 31일
답변 (1개)
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
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)
Vector = [00012,00402,01312,12345;00042,00420,missing,missing];
T2 = table (ID,Value1,Value2,Value3,Vector)
whos T1 T2
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
2023년 6월 1일
Cris LaPierre
2023년 6월 1일
You may not be able to.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!