How do I get the deletedElems to form its own column array? I got newMatrixA, but deletedElems keeps getting an output of 2 instead of [ 1; 3; 2; 9; 7; ]
10 views (last 30 days)
Show older comments
function [ newMatrixA, deletedElems ] = DeleteRowColumn( origMatrixA, delRow, delCol )
% DeleteRowColumn: Delete the row and column specified by delRow
% and delCol from input matrixA and returns the modified matrix and
% the deleted elements as a column array.
% Inputs: origMatrixA - input matrix
% delRow - row to delete
% delCol - column to delete
%
% Outputs: newMatrixA - input matrix with specified row and column deleted
% deletedElems - deleted elements from input matrix
% returned as a column array
% Assign newMatrixA with origMatrixA
newMatrixA = origMatrixA;
% Assign deletedElems with row of newMatrixA to be deleted
% (Hint: Use the transpose operator to convert a row to a column)
delRow = delRow(:)';
deletedElems = delRow;
% Remove row delRow from newMatrixA
newMatrixA(deletedElems,:) = [];
% Append deletedElems with the with column of newMatrixA to be deleted
deletedElems = delCol; % FIXME
% Remove column delCol from newMatrixA
newMatrixA(:,deletedElems) = []; % FIXME
end
0 Comments
Accepted Answer
Jan
on 13 Nov 2017
Edited: Jan
on 17 Nov 2017
Wanted: deletedElems - deleted elements from input matrix returned as a column array
function [ newMatrixA, deletedElems ] = DeleteRowColumn( origMatrixA, delRow, delCol )
% Assign newMatrixA with origMatrixA
newMatrixA = origMatrixA;
% Assign deletedElems with row of newMatrixA to be deleted
% (Hint: Use the transpose operator to convert a row to a column) *Strange hint?!*
tmp = newMatrixA(delRow, :);
deletedElems = tmp(:);
% Remove row delRow from newMatrixA
newMatrixA(delRow,:) = [];
% Append deletedElems with the with column of newMatrixA to be deleted
tmp = newMatrixA(:, delCol); % [EDITED]
deletedElems = cat(1, deletedElems, tmp(:));
% Remove column delCol from newMatrixA
newMatrixA(:, delCol) = [];
end
Or using logical indexing:
function [ newMatrixA, deletedElems ] = DeleteRowColumn( origMatrixA, delRow, delCol )
mask = false(size(origMatrixA));
mask(delRow, :) = true;
mask(:, delCol) = true;
deletedElems = origMatrixA(mask);
newMatrixA = reshape(origMatrixA(~mask), max(sum(mask, 1)), []);
end
3 Comments
Jan
on 17 Nov 2017
@Hena: cat concatenates the arguments along the the dimension specified as the first argument. cat(1, a, b) is the same as [a;b] . Then the line
deletedElems = cat(1, deletedElems, tmp(:))
appends the elements from removing the columns to the existing list of elements, which have been removed before.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!