How to insert a row in a matrix
조회 수: 55 (최근 30일)
이전 댓글 표시
I have a 31x 12 matrix, after the 11th row i want to insert a row matrix with zero values. so that i can make it 32x12 matrix.
how am i supposed to insert a zero row matrix after 11th row
댓글 수: 0
채택된 답변
Adam Danz
2019년 2월 19일
편집: Adam Danz
2019년 2월 25일
Here's an example.
data = rand(31,12); % your original matrix
newRow = zeros(1,size(data,2)); % row of 0s
newData = [data(1:11, :); newRow; data(12:end, :)] % your updated matrix
댓글 수: 1
John
2022년 12월 21일
This also works:
% Adding text to the middle of a text file.
file = ["I have a 31x 12 matrix,";" after the 11th row i want to insert ";...
"a row matrix with zero values.";"so that i can make it 32x12 matrix."];
Insert_string = "The quick brow fox";
i = 2;
file = [file(1:i,:); Insert_string; file(i+1:end, :)];
file =
5×1 string array
"I have a 31x 12 matrix,"
" after the 11th row i want to insert "
"The quick brow fox"
"a row matrix with zero values."
"so that i can make it 32x12 matrix."
추가 답변 (1개)
Jorge Fonseca
2022년 6월 22일
Hi Adam,
What if the "data" table as variable names? I have a table that I imported from Excel with variable names and i can add a row of zeros.
Regards
Jorge
댓글 수: 2
Adam Danz
2022년 6월 22일
Here's a demo that starts with the imported table. For simplicity, I used numeric data for all table variables. When you're appending rows to a table, the variable types within each column may not change.
T = array2table(randi(9,5,4))
Option 1: Add a row of zeros using vertical concatination
% Convert the rows of 0s to a table using the same variable names as
% original table.
T0 = array2table(zeros(1,width(T)),'VariableNames', T.Properties.VariableNames)
% Concatinate tables
T2 = [T; T0]
Option 2: Add a row of zeros using indexing
T{end+1,:} = zeros(1,width(T))
Now you can move the row(s) of added data to some other row(s) in the table. This example moves the row of 0s to row 3 of the table.
T = T([1,2,end,3:end-1],:)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!