Getting only first component from vector/matrix output

I'm using ode45 to solve a second-order IVP and want to use deval to evaluate the solution.
Since the equation was second-order, the solution vector has two components. I am wondering if there's an easy way to just get only the first component without having to assign deval's output to a temporary variable first?
I would've expected syntax like (deval(sol,x))(1) to work but it doesn't.
The same question applies not just to deval, but to any function which returns a vector/matrix.

 採用された回答

Walter Roberson
Walter Roberson 2011 年 4 月 1 日

0 投票

subsref(deval(sol,x),struct('type','()','subs',1))
If you are doing this multiple times, then just use (e.g.,)
first = @(x) x(1);
first(deval(sol,x))

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 4 月 1 日

0 投票

No. MATLAB does not support cascading indexing (except for in structures; which wouldn't be applicable here).

3 件のコメント

Positronized
Positronized 2011 年 4 月 1 日
Yes, I realised this, but I was just wondering if there's a trick one can play to achieve the same effect.
Sean de Wolski
Sean de Wolski 2011 年 4 月 1 日
You can use Walter's function handle, that will do it in one line. The temporary variable will still have to be created; however, in addition to the creation of the function handle and the call to it. What's so bad about having to return a temp variable?
Positronized
Positronized 2011 年 4 月 1 日
Nothing in general, it's just that I was trying to hack together a quick 1-line function handle without creating an M-file. I'll accept Walter's answer.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by