find similar element in a matrix

I have several questions. First, I have several vectors with different dimensions. I want to put them all in one matrix. I would fill it with zero to make them the same size as follows.
Do you know how to do it?
X= [1 2 3 9 20 67 81 43 101 24;
43 42 88 20 43 67 101 0 0 0;
22 44 0 0 0 0 0 0 0 0;
10 20 67 43 101 0 0 0 0 0;
]
I have a matrix, I want to generate a vector of all elements that are similar in all raws of that matrix. Some raws might have nothing similar as other raws. I count those element similar as if they are similar (e.g. 22 is similar to 22) or they are + - 2 different (e.g. 22 is similar to 24 or 20) the dimension of each raw is different. for example The similarity should be at least between three raws that we select that element.
Then I want to get a vector of all similar elements in all raws as follows
Y=[20 43 67];

 採用された回答

Sara
Sara 2014 年 5 月 2 日

0 投票

First question: create
X = zeros(10,4);
then plug in you vectors, e.g.,
X(1,1:numel(v1)) = v1;
Second point (I'm sure it can be written in a more compact way but it works):
X= [1 2 3 9 20 67 81 43 101 24;...
43 42 88 20 43 67 101 0 0 0;...
22 44 0 0 0 0 0 0 0 0;...
10 20 67 43 101 0 0 0 0 0;...
] ;
n = unique(X(1,:));
X(X==0)=NaN; % So you don't consider the zeros
nrow = size(X,1)-1;
Y = zeros(numel(X),1);cont = 0;
for i = 1:numel(n)
x = X(2:end,:);
[row,~] = find(x >= n(i)-2 & x <= n(i)+2);
if(numel(unique(row)) == nrow)
cont = cont + 1;
Y(cont) = n(i);
end
end
Y = Y(1:cont)

7 件のコメント

Niki
Niki 2014 年 5 月 2 日
Thanks but does not work because If you run this, it gives you an Empty matrix :-(
Sara
Sara 2014 年 5 月 2 日
With the matrix X provided it gives the right numbers to me. Are you using a different matrix?
Niki
Niki 2014 年 5 月 2 日
No, I use the same matrix but it is empty
Sara
Sara 2014 年 5 月 2 日
Is X or Y empty?
Niki
Niki 2014 年 5 月 3 日
Y is empty because I build the X myself as you explained in the first step (which works perfectly) but the second step when I want to generate the Y vector (similar elements) does not work. it give an empty matrix :-(
Sara
Sara 2014 年 5 月 3 日
Can you post the vectors you are using and how X looks like after you build it?
Niki
Niki 2014 年 5 月 3 日
I think i found where the problem was. If you check the first line, you are building a matrix of 10*4. however, I succeeded to solve the problem. Thanks. I will accept your code.

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2014 年 5 月 2 日

0 投票

As for your first question, take a look at my PADCAT function on the File Exchange as it does exactly what you want. http://www.mathworks.com/matlabcentral/fileexchange/22909-padcat
As for your second question, if you consider 22 to be the same as 20 and 24, are 20 and 24 then also the same?

3 件のコメント

Niki
Niki 2014 年 5 月 2 日
only + and - 2 between the element it was an example data but yes if you look at it like that
Jos (10584)
Jos (10584) 2014 年 5 月 3 日
So, 1 is the same as 3 which is the same as 4 which is … the same as … is the same as 123456789… !
Are you looking for some kind of cluster analysis? I think you need to be more precise in your goal.
Sara
Sara 2014 年 5 月 3 日
I think you take one element per time from a row and look if there are elements within +-2 in all the other lines. Then you start over with a new element.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2014 年 5 月 2 日

コメント済み:

2014 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by