Merge two cells into a single cell?

Hello World,
I'm basically trying to find the most frequent column vector of the vertically concatenated matrix of
x=[2 3 4 3 5 4 4]
y=[7 9 8 9 7 3 7]
I tried doing
z=[x;y]
mode(z,2)
But this only gives me the most frequent value for each row. I need the frequent combination ( in my case, 3;9 not 4;7)
So, right now I'm trying to find a way to make a matrix such that I'll get the following using x and y:
w=[27 39 48 39 57 43 47]
How could I do that?

1 Commento

So, since my real goal is to find the most frequent combination, and not to merge them, I used this code to finally get it. There probably is an easier way.
clear all
clc
a=zeros(1,7);
x=[25 3 4 3 5 4 4]
y=[7 9 8 9 7 3 7]
z=[x;y]
for i=1:7
for j=1:7
if (z(1,j)==z(1,i))&&(z(2,j)==z(2,i))
a(i)=a(i)+1;
end
end
end
a
maxim=max(a)
[locate]=find(a==maxim)
freq=z(:,locate(1))

Accedi per commentare.

Risposte (3)

Mischa Kim
Mischa Kim il 16 Mar 2014
Modificato: Mischa Kim il 16 Mar 2014
TUD, use
w = str2num(num2str([x' y'], '%-d'))'

2 Commenti

TUD
TUD il 16 Mar 2014
Thanks for responding Mischa. When I run the code you gave, I still get a 2x7 matrix. I want it as a 1x7 matrix.
Mischa Kim
Mischa Kim il 16 Mar 2014
Modificato: Mischa Kim il 16 Mar 2014
TUD, this should do the job:
x = [25 3 4 3 5 4 4];
y = [ 7 9 8 9 7 3 7];
z = str2num(strcat(num2str(x'),num2str(y')))
z =
257
39
48
39
57
43
47

Accedi per commentare.

w = 10*x + y

1 Commento

TUD
TUD il 16 Mar 2014
Thanks Jos. This does work, but it depends on the size of the values, that is, if the y is double or single digit, etc. I want this for decimals too.

Accedi per commentare.

Another option:
x=[2 3 4 3 5 4 4]
y=[7 9 8 9 7 3 7]
[xy,~,ix] = unique([x(:) y(:)],'rows') ;
[~,ix2] = mode(ix)
MyMode = xy(ix2,:).'

1 Commento

Jos,
This is the result I get:
x =
2 3 4 3 5 4 4
y =
7 9 8 9 7 3 7
ix2 =
2
MyMode =
3
While I need
Mymode= [3; 9]
I realized that I don't really need to find the best combination for my problem. But anyway, I still have added an edit that gets the best combination if anyone needs it.
Thanks for you help!

Accedi per commentare.

Categorie

Scopri di più su Operators and Elementary Operations in Centro assistenza e File Exchange

Richiesto:

TUD
il 16 Mar 2014

Commentato:

TUD
il 20 Mar 2014

Community Treasure Hunt

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

Start Hunting!

Translated by