MATLAB Answers

## How to read elements from a computed matrix without define a new variable?

Asked by Yize Wang

### Yize Wang (view profile)

on 10 May 2019
Latest activity Edited by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 10 May 2019
Accepted Answer by Stephen Cobeldick

### Stephen Cobeldick (view profile)

Dear all,
I am having a question about matrix computation. For example, a matrix A is calculated by an expression, . I am only interested in the element . Simply I can do and . I am wondering whether there is a way to do this without defining A. This line of code is not legal in MATLAB but it shows my goal clearly, .
Thanks!

Torsten

### Torsten (view profile)

on 10 May 2019
The matrix exponential of a matrix is not equal to the matrix where all of its elements are "exponentialized".
gonzalo Mier

### gonzalo Mier (view profile)

on 10 May 2019
Yize Wang, this is the easiest way and fastest to do it as I know. If you are concern about legibility, you can encapsulate this piece of code in a function called get_1_comp_system or some cool name (I'm not good at all naming).
Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 10 May 2019
"Eeeeeemmmm, Torsten... this equation is true always..."
Except that you did not take into account the matrix exponential shown in the original question.

Sign in to comment.

R2019a

## 1 Answer

### Stephen Cobeldick (view profile)

Answer by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 10 May 2019
Edited by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 10 May 2019
Accepted Answer

MATLAB does not allow indexing into the results of operations. You can either use a temporary variable (which is probably the most efficient solution), or you could call subsref directly:
>> B = rand(3);
>> C = rand(3);
>> D = rand(3);
>> a = subsref(expm(B*C+D),substruct('()',{1,1}))
a = 8.6593
>> A = expm(B*C+D) % for comparison:
A =
8.6593 7.3898 6.2134
13.1945 16.0237 11.6736
13.1316 13.8705 11.8273

#### 0 Comments

Sign in to comment.