MATLAB Answers

How to delete specific values from matrix

24 views (last 30 days)
omar alzouhayli
omar alzouhayli on 5 Jun 2021
Commented: dpb on 6 Jun 2021
I have image matrix 420x700 , and i want to delete a specific value in each row "change image dimensions",its like deleting a column from it "not in a straight line" to become 420x699 image , so i should Keep the values before the deleted value "horizontally" and Shift all the values after it back with 1 index
RGB = imread('image.jpg');
I1 = RGB(:,:,1);
how do i do that ?
  2 Comments
omar alzouhayli
omar alzouhayli on 5 Jun 2021
no , each value that i want to delete have different row and column index , but there is no more than one value in the same row

Sign in to comment.

Answers (3)

Bandar
Bandar on 5 Jun 2021
Edited: Bandar on 5 Jun 2021
You can't delete a single element in a matrix. This will affect the structure of the matrix. You could delete an entire row or column though. You may consider replacing an element with NaN. See the following
A=magic(3)
%Remove the second row
A(2,:) = []
%Assign element (2,2)
A(2,2) = NaN;
A
  1 Comment
dpb
dpb on 5 Jun 2021
The problem isn't to delete only a single element in the array, but one element from each row. That reduces the size of the array by one column just as if all were in the one column which is perfectly legal operation. It just can't be done one element at a time unless convert to a cell array to hold the temporary results along the way.
See Answer below for how to go about the process...alternatively, one could consider rearranging the positions of the elements in each row to line up the elements to be removed (to the last column, say) and then delete that column. Since it is an image OP is working with, however, that would then take rearranging the columns to put the shifted elements back in place to not distort the image so seems to be more work than proposed solution.
ADDENDUM: Of course, replacing each element first with NaN would let one then write
A(isnan(A))=[];
when all rows are complete. This will also internally work in linear addressing mode so one will also need the reshape to cast back to 2D.

Sign in to comment.


dpb
dpb on 5 Jun 2021
Edited: dpb on 5 Jun 2021
>> A=randi(30,5,5) % sample array to illustrate
ans =
23 4 7 27 8
9 15 23 29 26
21 29 8 17 8
20 11 16 5 25
5 18 21 5 8
>> [~,ic]=max(A,[],2) % pick a set of column indices to remove
ic =
4
4
2
5
3
>> ir=1:size(A,1).'; % need a row index vector to associate with ic
>> A(sub2ind(size(A),ir,ic))=[]; % remove the elements in linear indexing mode
>> A=reshape(A,numel(ir),[]) % reshape back to the 2D array
23 4 23 5
9 15 8 8
21 11 16 26
20 18 17 8
5 7 5 8
>>
illustrates the process -- you have a differing set of column indices generated from some other operation is only difference.

Image Analyst
Image Analyst on 6 Jun 2021
I believe this will do what you're asking for:
% Create sample data:
m = reshape(1:54, 6, [])
% Define columns from each row that we'd like to delete.
columnsToDelete = [1,2,3,4,5,6] % One for each row in m
% Define output maxtrix
m2 = m(:, 1:end-1);
% Scan down rows assigning from the next element after the skipped column
for row = 1 : size(m2)
m2(row, columnsToDelete(row):end) = m(row, columnsToDelete(row)+1:end);
end
m2
You'll see that m2 does not have the element from the column we told it to skip. So the 1 is missing from row 1, the 8 in column 2 is missing from row 2, the 15 in row 3 is missing in m2's row 3, and so on.
m =
1 7 13 19 25 31 37 43 49
2 8 14 20 26 32 38 44 50
3 9 15 21 27 33 39 45 51
4 10 16 22 28 34 40 46 52
5 11 17 23 29 35 41 47 53
6 12 18 24 30 36 42 48 54
columnsToDelete =
1 2 3 4 5 6
m2 =
7 13 19 25 31 37 43 49
2 14 20 26 32 38 44 50
3 9 21 27 33 39 45 51
4 10 16 28 34 40 46 52
5 11 17 23 35 41 47 53
6 12 18 24 30 42 48 54
  3 Comments
dpb
dpb on 6 Jun 2021
Yeah, thanks.
All the more reason for the first... :)

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!