Randomly delete elements of a matrix

조회 수: 8 (최근 30일)
Pascal Schulthess
Pascal Schulthess 2013년 4월 12일
댓글: Yueting Ding 2019년 6월 22일
Hi guys,
how would you, if you have an arbitrary matrix, randomly delete one element in each row and column of said matrix.
So, for example, you start with a matrix A of size 5x5 and you wanna end up with B of size 4x4.
Thanks!
Edit: I tried it like this, but it results in an error:
a = magic(5);
inRow = randperm(size(a,1));
inCol = randperm(size(a,2));
a(inRow,inCol) = [];
  댓글 수: 1
Mahdi
Mahdi 2013년 4월 12일
편집: Mahdi 2013년 4월 12일
What you're suggesting is a bit contradictory. If you delete one element in a 5x5, you would have the chosen element gone (make it a zero for example). But to make it a 4x4, you need to delete the WHOLE row or column. Please clarify.

댓글을 달려면 로그인하십시오.

답변 (4개)

Justace Clutter
Justace Clutter 2013년 4월 12일
The problem with what you have tried is that you have to remove the entire column and the row and not just a single element. Try the following code instead
a = magic(5);
rowIndex = randi(size(a, 1), 1);
columnIndex = randi(size(a, 2), 1);
anew = a;
anew(rowIndex,:) = [];
anew(:,columnIndex) = [];
disp(a);
disp(anew);
The Result of this code is the following:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
17 24 1 8
23 5 7 14
4 6 13 20
10 12 19 21
  댓글 수: 1
Yueting Ding
Yueting Ding 2019년 6월 22일
Thank you for your answer! It helped!

댓글을 달려면 로그인하십시오.


Justace Clutter
Justace Clutter 2013년 4월 12일
편집: Justace Clutter 2013년 4월 12일
I have a few followup questions:
1: What do you do in the following case?
x o o x
x x x o
x x x o
Should it be:
x x x x
x x x
x
In the example you provided, it was clean, but I am not sure what to do in this case.
2: Does the order of the elements in the final matrix matter?
  댓글 수: 1
Pascal Schulthess
Pascal Schulthess 2013년 4월 12일
  1. This case shouldn't appear because I said that I want to delete one (and only one) element in each column.
  2. The order in the columns doesn't matter, but row order should stay the same.
I've now implemented it like this:
v = magic(5);
in = randi(5,1,5);
vNew = zeros(size(v,1)-1, size(v,2));
for i = 1:length(in)
vTemp = v(:, i);
vTemp(in(i)) = [];
vNew(:, i) = vTemp;
end
This works well, but I thought that there should be a simpler way.

댓글을 달려면 로그인하십시오.


Cedric
Cedric 2013년 4월 12일
편집: Cedric 2013년 4월 12일
Look at the following and let me know if you have any question:
M = randi(10, 4, 5) ; % Random example.
[nr,nc] = size(M) ;
ind = sub2ind([nr,nc], randi(nr, 1, nc), 1:nc) ;
N = M(:) ;
N(ind) = [] ;
N = reshape(N, [], nc) ;
Running this gives e.g.:
>> M
M =
3 2 6 6 6
7 5 3 7 2
7 10 8 9 2
2 4 3 10 3
>> N
N =
3 2 6 7 6
7 10 3 9 2
7 4 8 10 2
PS: if you want to replace M with its reduced version, you don't need to build N; you can just have
[nr,nc] = size(M) ;
ind = sub2ind([nr,nc], randi(nr, 1, nc), 1:nc) ;
M = M(:) ;
M(ind) = [] ;
M = reshape(M, [], nc) ;
  댓글 수: 2
Justace Clutter
Justace Clutter 2013년 4월 12일
I was thinking about this solution but I was not sure if that is what he was looking for exactly. I was building up to it but you beet me to it. :)
Cedric
Cedric 2013년 4월 12일
Sorry for this, it happens to me as well occasionally ;-)

댓글을 달려면 로그인하십시오.


Pascal Schulthess
Pascal Schulthess 2013년 4월 12일
Thanks for the quick answers. Maybe I do need to clarify:
Let's say I have a 3x4 matrix which looks like:
x x x x
x x x x
x x x x
and, now I randomly want to delete one element in each column (in the following denoted by o) such that
x o x x
o x o x
x x x o
which then should result in a 2x4 matrix. I know I can do that with for loops, but I was hoping for an easier solution using indexing.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by