How to loop through a matrix using values from a vector

This is what I have so far :
function [x, y] = make1DGrid(n,l)
%L (lenght of domain)
%Nel (Number of elements)
x = linspace(0,l,n)
y = vec2mat(x, 2)
end
>> make1DGrid(11,10)
x =
0 1 2 3 4 5 6 7 8 9 10
y =
0 1
2 3
4 5
6 7
8 9
10 0
ans =
0 1 2 3 4 5 6 7 8 9 10
but my matrix needs to look like this :
y =
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

回答 (1 件)

Matt J
Matt J 2017 年 9 月 29 日

0 投票

y=[x(1:end-1);x(2:end)].';

2 件のコメント

Ale
Ale 2017 年 9 月 29 日
Thank you very much!
Matt J
Matt J 2017 年 9 月 29 日
You're welcome, but if this resolves your question, please click "Accept".

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

Ale
2017 年 9 月 29 日

コメント済み:

2017 年 9 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by