How to delete repeated column in this matrix?

a= [6.7747 15.1502 126.0000
67.9227 33.3699 74.0000
54.9636 40.7709 74.0000
102.5013 144.4162 44.0000
67.9227 33.3699 74.0000
54.9636 40.7709 74.0000];
i should not get the repeated values and i should get like this.
b=[6.7747 15.1502 126.0000
67.9227 33.3699 74.0000
54.9636 40.7709 74.0000
102.5013 144.4162 44.0000];

4 Commenti

There are no repeated columns in that matrix. The example you gave was for a repeated row. Did you mean that instead? Or do you really mean column like you said?
sir, check 'a' values
"[67.9227 33.3699 74.0000
54.9636 40.7709 74.0000]"
this two is repeated.
I believe he refers to the fact that a matrix consist of rows and columns and that your example is, in contrast to what you state, referring to duplicate rows instead of columns ;)
ya that's right it's not a matrix it's a value that's consist three variables like this
'x' y z
[67.9227 33.3699 74.0000];
but i'm considering only rows not columns in above my example.

Accedi per commentare.

 Risposta accettata

b = unique(a,'rows','stable');
b =
6.7747 15.1502 126
67.9227 33.3699 74
54.9636 40.7709 74
102.5013 144.4162 44
That should do it.

8 Commenti

This is not working...
Why? Be more explicit.
From your response I assume that you have a matlab version older than R2012a.
In that case, you can do the following:
[~,x,~] = unique(a,'rows','first');
b = a(sort(x),:);
And if you do not care about the order of your rows, simply doing unique(a,'rows') suffices.
still i'm not getting correct answer. k just see my code i attached below, just run my code, you get that repeated matrix that's the matrix what i discussed above. so in this i should not get repeated rows.
I checked your code and you seem to be doing something wrong in there.
If you actually have a matrix where you take the unique rows from, it will work. But instead your S_nodes variable is a vector which is overwritten in every iteration in the for loop @ line 308-325.
It seems like the numbers you print to the screen are the rows you obtain. What you should do is:
  1. Store these rows in a large matrix for every iteration.
  2. After all iterations are done, perform the unique operation and eliminate the duplicates.
Matlab111
Matlab111 il 30 Gen 2015
Modificato: Matlab111 il 30 Gen 2015
ya i understand what your saying and i tried that i'm not getting and actually i don't know how to store that rows.
If you know beforehand how many rows you will obtain, you could do something like this:
allRows = zeros(N,3); % put this before line 308 where your loop starts. N is the number of rows you will expect.
Then replace your line b = S_nodes(sort(x),:); with
allRows(i,:) = S_nodes;
And finally place your uniqueness check after line 325 (where the loop ends):
b = unique(allRows,'rows','stable');
disp(b);
or
[~,x,~] = unique(S_nodes,'rows','first');
b = S_nodes(sort(x),:);
disp(b);
If you don't know how many rows you will obtain, you can do it in a slightly more dirty way:
allRows = []; % put this before line 308 where your loop starts. N is the number of rows you will expect.
And in your loop:
allRows(end+1,:) = S_nodes;
The rest can be done in the same fashion as above.
This should be sufficient to get you started.
Matlab111
Matlab111 il 30 Gen 2015
Modificato: Matlab111 il 30 Gen 2015
ya Thank you i'm getting...

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 30 Gen 2015

Modificato:

il 30 Gen 2015

Community Treasure Hunt

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

Start Hunting!

Translated by