Delete a row from a matrix and save it in a new matrix

13 ビュー (過去 30 日間)
John Hock
John Hock 2019 年 5 月 1 日
回答済み: Walter Roberson 2019 年 5 月 1 日
Hi Everyone
I have a matrix lets say 10*10
I just want to remove a row from the matrix(this is done using
A(2,:) = []
where A is the matrix and 2 is the number of row to be removed
I just want to learn how to save the removed row i.e 2nd row in a new matrix.
Thanks in advance

回答 (2 件)

Guillaume
Guillaume 2019 年 5 月 1 日
Just copy the row before deleting it. After you've deleted it, it's gone for good and can no longer be retrieved.
B = A(2, :);
A(2, :) = [];

Walter Roberson
Walter Roberson 2019 年 5 月 1 日
saved_row = A(2,:);
A(2,:) = [];
There is no way to do this in one step, but you could write a function for it
function [A, removed] = delete_rows(A, whichrow)
removed = A(whichrow, :);
A(removed,:) = [];
end
and then
[A, saved_rows] = delete_rows(A, [2 19]);

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by