How to divide Vectors of different length?

3 ビュー (過去 30 日間)
krishnamurthy
krishnamurthy 2013 年 4 月 9 日
I have declared vectors 'a' and 'b'
Now the I want divide vector 'a' by vector 'b' and the result must store it in vector 'c'
The problem is vectors 'a' and 'b' are different lengths
But I managed by using the code given below;
Any one can try to assist this code to rewrite in a general format
a=linspace(1,16,16);
b=linspace(1,4,4);
c1 = a(1:4)./b(1);
c2 = a(5:8)./b(2);
c3 = a(9:12)./b(3);
c4 = a(13:16)./b(4);
c=[c1 c2 c3 c4];
c=[1,2,3,4,2.5,3,3.5,4,3,3.33,3.66,4,3.25,3.50,3.75,4;]
  1 件のコメント
Yao Li
Yao Li 2013 年 4 月 9 日
Is the length of a always an integral multiple of the length of b? It's important because we must first find out how many parts a is divided to.

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

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 9 日
編集済み: Azzi Abdelmalek 2013 年 4 月 9 日
a=linspace(1,16,16);
b=linspace(1,4,4);
d=reshape(a,[],numel(b))
out=bsxfun(@rdivide,x,b)
c=out(:)'
  1 件のコメント
Yao Li
Yao Li 2013 年 4 月 9 日
x should be d ^_^
This works well only the length of a is an integral multiple of the length of b.

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


Andrei Bobrov
Andrei Bobrov 2013 年 4 月 9 日
c = a./kron(b,[1 1 1 1]);

カテゴリ

Help Center および File ExchangeNumerical Integration and Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by