Merge two cells into a single cell?

2 views (last 30 days)
TUD
TUD on 16 Mar 2014
Commented: TUD on 20 Mar 2014
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 Comment
TUD
TUD on 16 Mar 2014
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))

Sign in to comment.

Answers (3)

Mischa Kim
Mischa Kim on 16 Mar 2014
Edited: Mischa Kim on 16 Mar 2014
TUD, use
w = str2num(num2str([x' y'], '%-d'))'
  2 Comments
TUD
TUD on 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 on 16 Mar 2014
Edited: Mischa Kim on 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

Sign in to comment.


Jos (10584)
Jos (10584) on 16 Mar 2014
w = 10*x + y
  1 Comment
TUD
TUD on 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.

Sign in to comment.


Jos (10584)
Jos (10584) on 16 Mar 2014
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 Comment
TUD
TUD on 20 Mar 2014
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!

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!