I have a problem with removing the rows when the columns are identical.
I have used a for and if loop but the run time is too long.
I was thinking if there are any more efficient and faster run time method.
say
A=[ 2 4 6 8;
3 9 7 9;
4 8 7 6;
8 5 4 6;
2 10 11 2]
I would want the result to be
A=[ 2 4 6 8;
4 8 7 6;
8 5 4 6]
eliminating the 2nd row because of the repeated '9' and remove the 5th row because of repeated '2'.
You can use sort and diff to identify the rows with repeated values
A = A(all(diff(sort(A'))),:)
returns
A =
2 4 6 8
4 8 7 6
8 5 4 6
The trick here is how to find the rows with repeated values in an efficient manner.
How about this:
% compare all-vs-all for each row using `bsxfun`
>> c = bsxfun( #eq, A, permute( A, [1 3 2] ) );
>> c = sum( c, 3 ); % count the number of matches each element has in the row
>> c = any( c > 1, 2 ); % indicates rows with repeated values - an element with more than one match
>> A = A( ~c, : )
A =
2 4 6 8
4 8 7 6
8 5 4 6
Related
Here, I subtract 2000 from column 2 and return the complete 3 column vector...
This "works"; but, isn't it processing the matrix 3 times?
xx = [X(:,1),X(:,2) .-2000,X(:,3)]
Best practice please... ;-0
The simplest way to do this operation is to simply:
X(:,2) -= 2000;
which is also a lot easier to read. This will modify the second column X "in place". If you want to make a copy of it where the second column is subtracted, then simply:
xx = X;
xx(:,2) -= 2000;
An example:
octave-cli-3.8.2> X = randi (9, 5, 3)
X =
1 4 4
1 2 6
8 4 3
7 7 1
7 7 2
octave-cli-3.8.2> X(:,2) -= 10
X =
1 -6 4
1 -8 6
8 -6 3
7 -3 1
7 -3 2
i would like to sum all the values from my 2nd column which have the same value in the first column.
So my matrix looks maybe like this:
column: [1 1 1 2 2 3 3 3 3 4 5 5]
column: [3 5 8 2 6 4 0 6 1 0 2 6]
now i would like to have for the value 1 in the 1st column a sum of 3, 5 and 8 in the 2nd column, the same goes for 2, 3 and so from the 1st column.
Like this for example:
[1 2 3 4 5],
[16 8 11 0 8]
i'm thankful for any suggestions!
Sum all values when values are equal :
Just to init :
a = [1 1 1 2 2 3 3 3 3 4 5 5 ; 3 5 8 2 6 4 0 6 1 0 2 6];
a = a.';
Let's go :
n=0
for i=1:size(a,1)
if a(i,1) == a(i,2)
n = n + a(i,1)
end
end
n
For the second question :
mat=0
for j = 1:max(a(:,1))
n=0
for i=1:size(a,1)
if j == a(i,1)
n = n + a(i,2)
end
end
mat(j,1) = j
mat(j,2) = n
end
mat
Result :
mat =
1 16
2 8
3 11
4 0
5 8
I want to sort in Matlab the element of each row of a matrix A in a matrix B and obtain a matrix C reporting the column index of each sorted element in the original matrix A. If two elements of a row of A are the same the reported column indices should be in increasing order, e.g.
A=[3 2 1 4; 5 6 7 8; 9 0 10 2; 2 1 1 0]
B=[1 2 3 4; 5 6 7 8; 0 2 9 10; 0 1 1 2]
C=[3 2 1 4; 1 2 3 4; 2 4 1 3; 4 2 3 1]
The builtin sort function will do this, when ran on rows (dimension 2 in Matlab).
First output will be the elements sorted within each row giving B
Second output will be the column indices of the elements of B from A within each row giving C
[B,C]=sort(A,2)
or if you just want C replace B with ~ in the above line..
suppose I have the following matrix
a =
2 NaN NaN
4 NaN 3
3 7 9
5 12 5
8 10 8
12 5 10
I need to replace all nan values with the first following non-nan element(column wise). The desired new matrix should be:
b =
2 7 3
4 7 3
3 7 9
5 12 5
8 10 8
12 5 10
Any ideas on how to do this in a general way?
Thank you in advance, Marios
Define the example data:
a = [
2 NaN NaN;
4 NaN 3;
3 7 9;
5 12 5;
8 10 8;
12 5 10;
];
% Here's the code:
b = a;
% Loop through all columns and all rows from bottom to top.
% If current element is not NaN and the element above is NaN,
% copy the value of current element to element above.
% If there are consecutive NaNs in the bottom of any column, they are not changed.
for colIndex = 1:size(b,2)
for rowIndex = size(b,1):-1:2
CurrentValue = b(rowIndex, colIndex);
if ~isnan(CurrentValue) && isnan(b(rowIndex-1, colIndex))
b(rowIndex-1, colIndex) = CurrentValue;
end
end
end
I have array say "a"
a =
1 4 5
6 7 2
if i use function
b=sort(a)
gives ans
b =
1 4 2
6 7 5
but i want ans like
b =
5 1 4
2 6 7
mean 2nd row should be sorted but elements of ist row should remain unchanged and should be correspondent to row 2nd.
sortrows(a',2)'
Pulling this apart:
a = 1 4 5
6 7 2
a' = 1 6
4 7
5 2
sortrows(a',2) = 5 2
1 6
4 7
sortrows(a',2)' = 5 1 4
2 6 7
The key here is sortrows sorts by a specified row, all the others follow its order.
You can use the SORT function on just the second row, then use the index output to sort the whole array:
[junk,sortIndex] = sort(a(2,:));
b = a(:,sortIndex);
How about
a = [1 4 5; 6 7 2]
a =
1 4 5
6 7 2
>> [s,idx] = sort(a(2,:))
s =
2 6 7
idx =
3 1 2
>> b = a(:,idx)
b =
5 1 4
2 6 7
in other words, you use the second argument of sort to get the sort order you want, and then you apply it to the whole thing.