how to create a matrix from a part of another matrix?

x= [1 54 61 23;71 4 79 33;12 34 65 89;]
I need to have y=[1 54;71 4] i meen, "y" must take the values X (1,1) (1,2) (2,1) (2,2)
how can i do it????

 채택된 답변

Star Strider
Star Strider 2014년 9월 7일
You essentially already did it. You simply need to use the correct index references:
x= [1 54 61 23;71 4 79 33;12 34 65 89];
y = x(1:2, 1:2);

댓글 수: 4

thanks and sorry, i didnt expresses what i nedded.
Ngl=2
x= [1 54 61 23;71 4 79 33;12 34 65 89;]
I need y=size(Ngl,Ngl) and to take the first values of the x Matrix inside "y"
This code:
x = [1 54 61 23;71 4 79 33;12 34 65 89];
Ngl = 2;
y = x(1:Ngl, 1:Ngl)
produces:
y =
1 54
71 4
It does, however if Ngl=1 y=[1;71] while the correct answer would be y=[1]
Ngl=3 y=[1 54 61;71 4 79] correct y=[1 54 61;71 4 79;12 34 65]
That is not the result I get:
Ngl = 1
y = x(1:Ngl, 1:Ngl)
produces:
Ngl =
1
y =
1
and this code:
Ngl = 3
y = x(1:Ngl, 1:Ngl)
produces:
Ngl =
3
y =
1 54 61
71 4 79
12 34 65
If you want to add a limit to be certain ‘Ngl’ does not exceed the size of ‘x’, the code becomes:
Ngl = 3
Ngl = min([Ngl size(x)]);
y = x(1:Ngl, 1:Ngl)
With this check, ‘y’ will always be square, will start at the first row and column indices of ‘x’, and ‘y’ will not attempt to get values of ‘x’ that are beyond the index limits of ‘x’.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2014년 9월 7일

편집:

2014년 9월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by