What does the array mean?

1 visualización (últimos 30 días)
Shun Weng
Shun Weng el 30 de Sept. de 2015
Respondida: Chad Greene el 30 de Sept. de 2015
A (end+1 : end+2, 1:2 ) = eye(2), what does this array mean?
  1 comentario
per isakson
per isakson el 30 de Sept. de 2015
Find out:
>> A = ones(4,4)
A =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> A (end+1 : end+2, 1:2 ) = eye(2),
A =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 0 0 0
0 1 0 0

Iniciar sesión para comentar.

Respuestas (2)

John D'Errico
John D'Errico el 30 de Sept. de 2015
What does it mean? Nothing, besides the obvious. They are numbers. Actually, the array merely represents numbers.
Numbers, by themselves have no intrinsic meaning. It is to the person who creates such an array for which those numbers MAY have a meaning.

Chad Greene
Chad Greene el 30 de Sept. de 2015
The eye function creates the identity matrix. It's just ones in a diagonal line, with zeros everywhere else. So
>> eye(2)
ans =
1 0
0 1
creates the 2x2 identity matrix you see above,
>> eye(3)
ans =
1 0 0
0 1 0
0 0 1
creates a 3x3 identity matrix, and so on.
The bit of code you wrote assumes you already have some matrix A. Perhaps A is a 3x2 matrix of all fives:
>> A = 5*ones(3,2)
A =
5 5
5 5
5 5
It doesn't matter what exactly is inside A. But this bit:
A(end+1:end+2
says we're gonna change the size of A. It says starting at the bottom row (the end) of A, ranging from rows end+1 to end+2, we're gonna tack on some new numbers. And to be specific, those new numbers will be placed in columns 1 to 2. So
A(end+1:end+2,1:2) =
says put something in two new rows added the bottom of A in columns 1 and 2. What will we put in these newly-added rows of A? How about an identity matrix?
>> A(end+1:end+2,1:2) = eye(2)
A =
5 5
5 5
5 5
1 0
0 1
The A matrix no longer has the dimensions 3x2. Now it's 5x2. But because we're using the end command instead of explicitly stating which row number to write or over-write, we can keep adding new rows to A. Perhaps you want to put some threes down there:
>> A(end+1:end+2,1:2) = 3*ones(2)
A =
5 5
5 5
5 5
1 0
0 1
3 3
3 3
Or some arbitrary numbers:
>> A(end+1:end+2,1:2) = [87 42; 6.3 9]
A =
5.0000 5.0000
5.0000 5.0000
5.0000 5.0000
1.0000 0
0 1.0000
3.0000 3.0000
3.0000 3.0000
87.0000 42.0000
6.3000 9.0000

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by