How to insert vector to table?
조회 수: 98 (최근 30일)
이전 댓글 표시
for example, I want to insert vector into NoshellRings. I've tried but error occured.
It said that The value cannot be replaced because the number of elements on the left and right sides is different.
Please let me know how to solve this.
Thanks.
댓글 수: 1
dpb
2022년 9월 3일
You must either have exactly the same number of elements in the vector as height() of the table or have an indexing expression for the values to be replaced that contains exactly the same number of locations to replace as the number of elments in the vector. No tolerances allowed.
You've not provided enough information for us to be able to know what either of the vector size is, but the table has 4779 rows so to replace the whole variable the vector has to have 4779 elements, no more, no fewer.
You can replace parts of the whole thing, of course, but then you have to have something like
tVariableName.NoshellRings(1:3)=vector; % if your vector is 3-elements long and you want to replace the first 3
The indexing expression can be explict indices as above or a logical addressing vector, but the count simply has to match.
답변 (1개)
Robert U
2022년 9월 5일
Hi Sierra,
if you want to save vectors in table elements you can use cell arrays:
testTable = table;
testTable.Sex = ['M';'M';'F';'M';'I'];
testTable.Length = [0.455; 0.350; 0.53; 0.44; 0.33];
testTable.NoShellRings = [4; 7; 9; 10; 7];
testVector = [1 2 3 4 5];
testTable.NoShellRings = arrayfun(@(dIn) {dIn},testTable.NoShellRings);
testTable.NoShellRings{3} = testVector
If you want to replace portions of the table with values given in a vector you follow dpb's comment.
testTable = table;
testTable.Sex = ['M';'M';'F';'M';'I'];
testTable.Length = [0.455; 0.350; 0.53; 0.44; 0.33];
testTable.NoShellRings = [4; 7; 9; 10; 7];
testVector = [2 3 4; 6 7 8]; % testVector = [index; value];
testTable.NoShellRings(testVector(1,:)) = testVector(2,:)
Kind regards,
Robert
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!