Replace duplicate elements from vector with 0 (Matlab/Octave) - matlab

I want to replace duplicate elements from a vector with 0, and keep only the first occurrence.
If I have a vector like
[ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ]
how could I transform it into
[ 1 0 2 0 0 3 0 0 4 0 0 0 5 0 0 0 6 0 0 ] ?
Thanks.

a = [ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ];
[c, ia] = unique(a, 'first');
t = a;
t(ia) = 0;
filtered_vect = a - t;
edit: That in a more concise way, destroying the original vector:
a = [ 1 1 2 2 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 ];
[c, ia] = unique(a, 'first');
a(~ismember(1:length(a),ia)) = 0;

Related

MATLAB diff function with matrices of unequal dimensions

I read MATLAB diff.m readme, still couldn't figure out the following:
a = [1 2 3]'
b = [3 2 1 4; 1 1 1 5; 5 5 5 6]
diff([a b]') =
2 -1 2
-1 0 0
-1 0 0
3 4 1
what rule is MATLAB applying here? does MATLAB apply different rule if one of the matrices (i.e. a or b) is logical matrix? or both a and b are logical matrix?
MATLAB applies the same rule regardless of the input matrices. Run your code line-by-line in the command window and see.
a and b are like this:
>> a = [1 2 3]'
a =
1
2
3
>> b = [3 2 1 4; 1 1 1 5; 5 5 5 6]
b =
3 2 1 4
1 1 1 5
5 5 5 6
then [a b]':
>> [a b]'
ans =
1 2 3
3 1 5
2 1 5
1 1 5
4 5 6
Now apply the diff rule on this as follows:
[ row 2 - row 1 ]
[ row 3 - row 2 ]
[ row 4 - row 3 ]
[ row 5 - row 4 ]
you will get
>> diff([a b]')
ans =
2 -1 2
-1 0 0
-1 0 0
3 4 1
a =
[1
2
3]
So,
[a b] =
[ 1 3 2 1 4
2 1 1 1 5
3 5 5 5 6 ]
and thus
[a b]' =
[ 1 2 3
3 1 5
2 1 5
1 1 5
4 5 6 ]
and then diff takes the differences along the first dimension whose size is not 0 (i.e. down each column).
This gives the result
diff([a b]') =
[ 2 -1 2
-1 0 0
-1 0 0
3 4 1 ].

Find rows of a matrix whose certain columns all match a condition

Suppose I have a matrix with many rows and columns, for example a small subset would be:
1 2 3 4 5 6
1 1 5 6 0 0
1 2 2 3 2 1
1 2 0 3 4 5
1 9 5 7 3 0
I want to find the rows whose columns #4, #5 and #6 contain elements greater than zero, so I in this case would get a vector like this:
1
3
4
I have tried using the find() function this way:
idx = find(y(:, 4:6) > 0)
but I get this:
1
2
3
4
5
6
8
9
10
11
13
14
You can use a combination of find and all like this:
idx = find(all(y(:,4:6) > 0, 2))
This gives:
>> y = [1 2 3 4 5 6; 1 1 5 6 0 0; 1 2 2 3 2 1; 1 2 0 3 4 5; 1 9 5 7 3 0]
y =
1 2 3 4 5 6
1 1 5 6 0 0
1 2 2 3 2 1
1 2 0 3 4 5
1 9 5 7 3 0
>> idx = find(all(y(:,4:6) > 0, 2))
idx =
1
3
4
The idea is that we extract columns 4 to 6, check which values are greater than 0, operate along the 2nd dimension with an AND condition (all), and then extract which indices (rows) are 1/true in the resulting column vector.

All scaled combination of vector elements in Matlab

I have a vector like this:
A=[3 4 5 6];
I would like obtain a new Matrix B which is composed by all possible scaled combination of A elements avoiding rows with only one element (then at least two elements for each row), for instance:
B=[3 4 5 6;
3 4 5 0;
3 4 0 0;
0 4 5 6;
0 0 5 6;
3 0 5 6;
3 0 5 0;
0 0 5 6;
3 4 0 6;
0 4 0 6;
3 4 0 0;
etc...
];
Could you please help me?
Thanks in advance
Here's a way to do it:
A = [3 4 5 6]; % data
N = 2; % minimum number of elements that should be present
p = dec2bin(1:2^numel(A)-1)-'0'; % binary pattern. Each row is a combination
s = sum(p,2)>=N; % index to select rows of p that have at least N ones
result = bsxfun(#times, A, p(s,:)); % multiply with singleton expansion
This gives, in your example,
result =
0 0 5 6
0 4 0 6
0 4 5 0
0 4 5 6
3 0 0 6
3 0 5 0
3 0 5 6
3 4 0 0
3 4 0 6
3 4 5 0
3 4 5 6

Every possible sum combination in a vector

Assuming I'm having a vectors of numbers A, for example: A=[1 3 5 3 9 6](A's length >= 2) and an Integer X=6. Need to find how many pairs (A[i],A[j]) where i<j exist in the vector which answer this condition: A[i]+A[j]=X. The number of pairs is printed.
Not allowed to use for/while. Allowed only ceil,floor,mod,repmat,reshape,size,length,transpose,sort,isempty,all,any,find ,sum,max,min.
With repmat, length and sum -
integer1 = 6; %// One of the paramters
A_ind = 1:length(A) %// Get the indices array
%// Expand A_ind into rows and A_ind' into columns, to form a meshgrid structure
A_ind_mat1 = repmat(A_ind,[length(A) 1])
A_ind_mat2 = repmat(A_ind',[1 length(A)]) %//'
%// Expand A into rows and A' into columns, to form a meshgrid structure
A_mat1 = repmat(A,[length(A) 1])
A_mat2 = repmat(A',[1 length(A)]) %//'
%// Form the binary matrix of -> (A[i],A[j]) where i<j
cond1 = A_ind_mat1 < A_ind_mat2
%// Use the binary matrix as a logical mask to select elements from the two
%// matrices and see which element pairs satisfy -> A[i]+A[j]=X and get a
%// count of those pairs with SUM
pairs_count = sum((A_mat1(cond1) + A_mat2(cond1))==integer1)
Outputs from code run to make it clearer -
A =
1 3 5 3 9 6
A_ind =
1 2 3 4 5 6
A_ind_mat1 =
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
A_ind_mat2 =
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6
A_mat1 =
1 3 5 3 9 6
1 3 5 3 9 6
1 3 5 3 9 6
1 3 5 3 9 6
1 3 5 3 9 6
1 3 5 3 9 6
A_mat2 =
1 1 1 1 1 1
3 3 3 3 3 3
5 5 5 5 5 5
3 3 3 3 3 3
9 9 9 9 9 9
6 6 6 6 6 6
cond1 =
0 0 0 0 0 0
1 0 0 0 0 0
1 1 0 0 0 0
1 1 1 0 0 0
1 1 1 1 0 0
1 1 1 1 1 0
pairs_count =
2
A bit more explanation -
Taking few more steps to clarify why pairs_count must be 2 here -
Set all values in A_mat1 and A_mat2 to be zeros that do not satisfy the less than criteria
>> A_mat1(~cond1)=0
A_mat1 =
0 0 0 0 0 0
1 0 0 0 0 0
1 3 0 0 0 0
1 3 5 0 0 0
1 3 5 3 0 0
1 3 5 3 9 0
>> A_mat2(~cond1)=0
A_mat2 =
0 0 0 0 0 0
3 0 0 0 0 0
5 5 0 0 0 0
3 3 3 0 0 0
9 9 9 9 0 0
6 6 6 6 6 0
Now, add A_mat1 and A_mat2 and see how many 6's you got -
>> A_mat1 + A_mat2
ans =
0 0 0 0 0 0
4 0 0 0 0 0
6 8 0 0 0 0
4 6 8 0 0 0
10 12 14 12 0 0
7 9 11 9 15 0
As you can see there are two 6's and thus our result is verified.

MATLAB: How do you add all the first n rows of a matrix within the same column, and save the result to the last row?

It was hard to phrase the question, but here's an example of what I'm looking for:
1 2 3 4
2 1 1 1
2 2 3 1
0 0 0 0
and in column one, I add all the value of all of the first three rows and save it to the third and so on, so that it becomes:
1 2 3 4
2 1 1 1
2 2 3 1
5 5 7 6
I think you can use sum:
octave:23> m = [1 2 3 4; 2 1 1 1; 2 2 3 1; 0 0 0 0]
m =
1 2 3 4
2 1 1 1
2 2 3 1
0 0 0 0
octave:24> m(length(m), :) = sum(m)
m =
1 2 3 4
2 1 1 1
2 2 3 1
5 5 7 6