Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace the rows of an array having repeated number by a fixed given row

Tags:

matrix

row

matlab

I have a matrix having rows with repeated numbers. I want to find those rows and replace them with a dummy row so as to keep the number of rows of the matrix constant.

Dummy_row = [1 2 3]

(5x3) Matrix A

A = [2 3 6;
     4 7 4;
     8 7 2;
     1 3 1;
     7 8 2]

(5x3) Matrix new_A

new_A = [2 3 6;
         1 2 3;
         8 7 2;
         1 2 3;
         7 8 2]

I tried the following which deleted the rows having repeated numbers.

y = [1 2 3]
w = sort(A,2)
v = all(diff(t,1,2)~=0|w(:,1:2)==0,2)  %  When v is zero, the row has repeated numbers
z = A(w,:)

Can you please help?

like image 839
Amit Sookun Avatar asked Dec 14 '14 20:12

Amit Sookun


People also ask

How do you find the two repeating numbers in an array?

All elements of the array are in range 1 to n. And all elements occur once except two numbers which occur twice. Find the two repeating numbers. For example, array = {4, 2, 4, 5, 2, 3, 1} and n = 5. The above array has n + 2 = 7 elements with all elements occurring once except 2 and 4 which occur twice. So the output should be 4 2.

How to repeat an element from an array in Java?

so there is two number which is repeated. Step 1. Initialize arrays. Step 2. Declare the scanner class for taking input. Step 3. take array size from the user. Step 4. Take an element of the array from the user. Step 5. Call the Repeating method which gives us repeating element from the array.

How do you find the number of duplicates in an array?

Given an array of n + 1 integers between 1 and n, find one of the duplicates. If there are multiple possible answers, return one of the duplicates. If there is no duplicate, return -1. Example: Input: [1, 2, 2, 3]

How to find and replace objects in an array of objects?

As you can see, using findIndex we can easily find and then replace Objects in an Array of Objects. Let’s say we are not interested in replacing a value but we just want to remove it, we will now look at different ways of doing so. First, let’s look at the more basic methods to remove values from an Array: Array.pop and Array.shift


2 Answers

bsxfun based solution -

%// Create a row mask of the elements that are to be edited
mask = any(sum(bsxfun(@eq,A,permute(A,[1 3 2])),2)>1,3);

%// Setup output variable and set to-be-edited rows as copies of [1 2 3]
new_A = A;
new_A(mask,:) = repmat(Dummy_row,sum(mask),1)

Code run -

A =
     2     3     6
     4     7     4
     8     7     2
     1     3     1
     7     8     2
new_A =
     2     3     6
     1     2     3
     8     7     2
     1     2     3
     7     8     2
like image 73
Divakar Avatar answered Sep 29 '22 06:09

Divakar


You could use the following:

hasRepeatingNums = any(diff(sort(A, 2), 1, 2)==0, 2); 
A(hasRepeatingNums,:) = repmat(Dummy_row, nnz(hasRepeatingNums), 1);
like image 30
knedlsepp Avatar answered Sep 29 '22 07:09

knedlsepp