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

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))

댓글을 달려면 로그인하십시오.

답변 (3개)

Mischa Kim
Mischa Kim 2014년 3월 16일
편집: Mischa Kim 2014년 3월 16일

0 개 추천

TUD, use
w = str2num(num2str([x' y'], '%-d'))'

댓글 수: 2

TUD
TUD 2014년 3월 16일
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 2014년 3월 16일
편집: Mischa Kim 2014년 3월 16일
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

댓글을 달려면 로그인하십시오.

Jos (10584)
Jos (10584) 2014년 3월 16일

0 개 추천

w = 10*x + y

댓글 수: 1

TUD
TUD 2014년 3월 16일
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.

댓글을 달려면 로그인하십시오.

Jos (10584)
Jos (10584) 2014년 3월 16일

0 개 추천

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

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!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

TUD
2014년 3월 16일

댓글:

TUD
2014년 3월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by