Concatenate matrix numbers linspace
古いコメントを表示
Hi there,
I have a matrix variable x = [0 1 2 3]
I want to generates linearly spaced vectors in between the numbers into a variable. My problem here is concatenate the numbers into p the next time n increases.
I know i should be using linspace to generate number for eg:
for i = 1:(length(x)-1)
p = linspace(x(i),x(i+1),0.5)
end
the results i want is:
p = 0 0.5 1 1.5 2 2.5 3
Hope someone can shed some light here.
1 件のコメント
Azzi Abdelmalek
2012 年 8 月 13 日
what do you mean by
p = linspace(x(i),x(i+1),0.5)
採用された回答
その他の回答 (5 件)
p = 0:.5:3;
or
p = linspace(0,3,7)
EDIT.
I think I misunderstood your problem. Do you mean like this:
x = [0.25 1 1.5 2 2.4 2.6]
x(2,1:end-1) = diff(x)/2+x(1:end-1);
x = reshape(x(1:end-1),1,[])
Sean de Wolski
2012 年 8 月 13 日
編集済み: Sean de Wolski
2012 年 8 月 13 日
Here is a terrible solution:
x = 0:3; %sample x
x = [x(1:end-1); x(2:end)]; %each start/end pair
nAdd = 2; %add two elements between
xnew = interp1((1:2)',x,linspace(1,2,2+nAdd)); %interpolate (2d linspace)
xnew = vertcat(reshape(xnew(1:end-1,:),[],1),x(end)) %keep non-duplicate parts
Amazing Trans
2012 年 8 月 14 日
編集済み: Amazing Trans
2012 年 8 月 14 日
3 件のコメント
Amazing Trans
2012 年 8 月 14 日
Matt Fig
2012 年 8 月 14 日
Please close out this question by selecting a best answer then post a new question and link back to this one.
Sean de Wolski
2012 年 8 月 14 日
My answer handles your first scenario!
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!