フィルターのクリア

Help creating a vector in a loop

2 ビュー (過去 30 日間)
Liam Ryan
Liam Ryan 2019 年 10 月 2 日
コメント済み: Walter Roberson 2019 年 10 月 2 日
Hi, I want to create a vector in a loop from two other vectors:
A = [10 14 19 20]
B = [19 34 56 49]
Those two are the vectors at the top which I want to use to make another vector and I want to make a new vector such that it is like element by element operation using the following for loop:
for i = 1:length(A)
new_vector(i) = linspace(A(i),B(i),25)
end
So in the first iteration I want
new_vector = linspace(10,19,25)
since A(1) = 10, so first argument of linspace and B(1) = 19 second argument. But when I try doing this, it says:
Unable to perform assignment because the left and right sides have a different number of elements.
Help out guys

回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 10 月 2 日
new_vector{i} = linspace(A(i),B(i),25);
Notice the {} instead of ()
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 10 月 2 日
linspace(0,1,25).' .* (B-A) + A
No loop needed in this special case of the length of output vectors being consistent.

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


Andrei Bobrov
Andrei Bobrov 2019 年 10 月 2 日
n = numel(A);
new_vector = zeros(25,n);
for ii = 1:n
new_vector(:,ii) = linspace(A(ii),B(ii),25);
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by