Azzera filtri
Azzera filtri

Randomly delete elements of a matrix

9 visualizzazioni (ultimi 30 giorni)
Pascal Schulthess
Pascal Schulthess il 12 Apr 2013
Commentato: Yueting Ding il 22 Giu 2019
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 Commento
Mahdi
Mahdi il 12 Apr 2013
Modificato: Mahdi il 12 Apr 2013
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.

Accedi per commentare.

Risposte (4)

Justace Clutter
Justace Clutter il 12 Apr 2013
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

Justace Clutter
Justace Clutter il 12 Apr 2013
Modificato: Justace Clutter il 12 Apr 2013
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 Commento
Pascal Schulthess
Pascal Schulthess il 12 Apr 2013
  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.

Accedi per commentare.


Cedric
Cedric il 12 Apr 2013
Modificato: Cedric il 12 Apr 2013
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 Commenti
Justace Clutter
Justace Clutter il 12 Apr 2013
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 il 12 Apr 2013
Sorry for this, it happens to me as well occasionally ;-)

Accedi per commentare.


Pascal Schulthess
Pascal Schulthess il 12 Apr 2013
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.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by