If A is a 3x4 matrix, how do you find B if B=A(1:2)?
Afficher commentaires plus anciens
For example if A=[1 2 3 4; 5 6 7 8; 9 10 11 12]
B = A(1:2) = ???
And also how come the value of 1:2 in A(1:2, 2:3) is different than A(1:2)?
Réponse acceptée
Plus de réponses (2)
Albert
le 16 Jan 2017
0 votes
use function find, and your problem is solved. A=[1 2 3 4; 5 6 7 8; 9 10 11 12]; B = A(1:2); find(A==B(1)) >>1 which is the location of A==B(1) in matrix A.
A(1:2, 2:3) is different than A(1:2) A(1:2, 2:3) is short for A(1, 2), A(1, 3),A(2, 2), A(2, 3). A(1:2) is short for A(1, 1) and A(2, 1)
These are very basic and introductory knowledge, which you don't want to ask here.
1 commentaire
Niels
le 16 Jan 2017
for this Matrix A your solution may work, but imagine A had more than one 1 => the result wouldnt be unequivocal
Star Strider
le 16 Jan 2017
MATLAB uses linear indexing for arrays, because the arrays are stored as consecutive elements in memory in column-dominant representation. So in your matrix, the first element in the vector is 1, the second is 5, the third is 9, the fourth is 2, and so on.
So:
B = A(1:2) = [1 5]
If you choose to explore this convention:
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
B = A(1:2)
[row,col] = ind2sub(size(A), (1:2))
row =
1 2
col =
1 1
so the [row,column] indices for ‘A(1:2)’ are [1,1] (row #1, column #1) and [2,1] (row #2, column #1) respectively.
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!