How to make an index of the points of a vector that can then be used in a matrix

1 回表示 (過去 30 日間)
Ale
Ale 2017 年 9 月 30 日
コメント済み: Ale 2017 年 9 月 30 日
This is the function I have generated: function [Points, Elements] = make1DGrid(Nel,L) %L (Lenght of domain) %Nel (Number of elements)
Points = linspace(0,L,Nel)
Elements = [Points(1:end-1);Points(2:end)] end
my function produces: Points = [0 0.33 0.66 1] Elements = [0 0.33; 0.33 0.66; 0.66 1]
But instead of creating a matrix (Elements) where the coordinates of the points are used I need to create a matrix where the index of the points are used i.e for Nel=3, and L=1 Points = [0 0.33 0.66 1] Elements = [1 2; 2 3; 3 4]
  1 件のコメント
Jan
Jan 2017 年 9 月 30 日
For Nel=3 you do not get Points = [0 0.33 0.66 1], but this is Nel=4.

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

回答 (1 件)

Jan
Jan 2017 年 9 月 30 日
編集済み: Jan 2017 年 9 月 30 日
If you need the indices instead of the values, simply use the indices instead of the values :-)
function [Points, Elements] = make1DGrid(Nel,L)
Points = linspace(0, L, Nel);
Elements = [1:Nel-1; 2:Nel].'; % Instead of: [Points(1:end-1); Points(2:end)]
end
  1 件のコメント
Ale
Ale 2017 年 9 月 30 日
Thanks so much. I thought it had to be Nel but every time I used it I kept getting errors because I wasn't using the correct syntax so you've helped a lot!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by