Need command help for a specific matrix operation

hi,
I need the command help to do the following. Situation.
A = [2 3 4;7 8 1;3 2 7;1 9 3];
I want to square this matrix and then want to save the 2nd and 3rd row of the result matrix(4x3) matrix to another matrix of 2x3 dimension. I can do like this
B = A.^2; C = B(2:3,:); now C contains my required result.
But I want to avoid the intermediate step of first saving it in matrix B above. I want the C matrix directly from A. I need a single line command for above two statements.
I tried like following C = (A.^2)(2:3,:); But it is showing syntax error.
Please help me in this.

回答 (3 件)

Richard Brown
Richard Brown 2012 年 5 月 3 日

1 投票

EDIT Needed to strain my eyes harder to figure out that the matrix in question had 4 rows
C = [0 1 0 0; 0 0 1 0] * A.^2;

3 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 3 日
As A is 4 x 3, and the desired result is 2 x 3, the pre-multiplier for matrix multiplication would have to be 2 x 4, not 2 x 3.
Geoff
Geoff 2012 年 5 月 3 日
Don't blame you for mistaking that. The use of whitespace in computer languages is just as important as in human languages, even if it's not strictly necessary. I love languages like Python that recognise this and actually make whitespace part of the syntax!
Andrei Bobrov
Andrei Bobrov 2012 年 5 月 3 日
+1

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

Walter Roberson
Walter Roberson 2012 年 5 月 3 日

0 投票

This is the official mechanism for the kind of indexing of results that you want to do:
subsref(A.^2, struct('type', '()', 'subs', {{2:3, ':'}}))
You can wrap the functionality into anonymous functions to make it look more compact.

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2012 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by