Combining two different matrices of different sizes

I want to combine two different matrices with a different number of columns so that the output has the lower number of columns. I also want the columns that were "modified" to have the average value of what they were modified. I think these examples clarify what I'm trying to say:
For example, for matrices that are multiples of each other:
A=[5 6]
B=[1 2 3 4]
I would combine these matrices so
C=[5 6; 1.5 3.5]
Now if the matrices aren't multiples of each other, then it would combine to whichever value it finds fit, for example:
A=[5 6]
B=[1 2 3 4 5]
Then the output is:
C=[5 6; 2 4.5] (The first 3 values were averaged)
Thanks

2 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 10 日
What is the size of A?
Jan
Jan 2013 年 1 月 10 日
The question is not clear. What happens for other number of columns for A and C?

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

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 10 日
編集済み: Azzi Abdelmalek 2013 年 1 月 10 日

0 投票

A=[5 6]
B=[1 2 3 4 5]
m=numel(B);
m1=ceil(m/2)
C=[A;mean(B(1:m1)) mean(B(m1+1:end))]

2 件のコメント

Amith Kamath
Amith Kamath 2013 年 1 月 10 日
doesn't this work only with numel(A) == 2 ?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 10 日
編集済み: Azzi Abdelmalek 2013 年 1 月 10 日
Try this
A=[5 6 7]
B=[1 2 3 4 5 7 8 6 9 10 11]
n=numel(A)
m=numel(B);
m1=floor(m/n)
m2=m-m1*n+m1
C1=mean(B(1:m2))
for k=m2+1:m1:m
C1=[C1;mean(B(k:k+m1-1))]
end
C=[A;C1']
%or I prefer
A=[5 6 7 80 47]
B=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 25 35]
n=numel(A);
m=numel(B);
m1=floor(m/n);
h=zeros(1,n);
h(1:m-m1*n)=1
v2=cumsum(ones(1,n)*m1+h);
v1=[1 v2(1:end-1)+1];
C=[A;arrayfun(@(x) mean(B(v1(x):v2(x))),1:n)]
%In the first code the mean values were calculated for
[B(1:5) B(6:8) B(9:11)]
% In the second code
[B(1:4) B(5:8) B(9:11)]

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by