Write a row and column vector as matrix index

I want to write a row vector [4 5 3] and column vector [4 5 3]' as (4, 4) (4,5) 4, 3) (5, 4) (5, 5) (5, 3) (3, 4) (3, 5) and (3, 3) so that i can use it as index of a matrix.

1 comentario

"I want to write a row vector [4 5 3] and column vector [4 5 3]' as (4, 4) (4,5) 4, 3) (5, 4) (5, 5) (5, 3) (3, 4) (3, 5) and (3, 3) so that i can use it as index of a matrix."
Why do you need to "write" it like that to "use it as index of a matrix" ?
You can use those numeric vectors directly as indices:
A = rand(5,5)
A = 5×5
0.7590 0.3614 0.8548 0.3843 0.2101 0.5864 0.4088 0.1042 0.7392 0.7515 0.7229 0.3837 0.2563 0.8996 0.6743 0.8432 0.9548 0.7702 0.7944 0.3817 0.1770 0.8465 0.8390 0.8715 0.3211
X1 = [4,5,3];
X2 = [4,5,3];
B = A(X1,X2)
B = 3×3
0.7944 0.3817 0.7702 0.8715 0.3211 0.8390 0.8996 0.6743 0.2563

Iniciar sesión para comentar.

 Respuesta aceptada

a = [4 5 3];
% Generate index vectors A and B
[A,B] = ndgrid(a,a);
A = A(:)
A = 9×1
4 5 3 4 5 3 4 5 3
B = B(:)
B = 9×1
4 4 4 5 5 5 3 3 3
% Use A and B as index vectors for a matrix M
M = rand(10)
M = 10×10
0.7393 0.4634 0.5887 0.1407 0.1101 0.2377 0.3904 0.9721 0.7022 0.5305 0.4654 0.4633 0.4541 0.0660 0.8832 0.4376 0.1124 0.0514 0.7515 0.1212 0.1072 0.2151 0.8151 0.0369 0.0057 0.3753 0.1588 0.9982 0.6612 0.0060 0.0766 0.8351 0.3845 0.1705 0.2370 0.4290 0.2396 0.6795 0.1800 0.3630 0.1256 0.2626 0.1554 0.5475 0.3741 0.0379 0.8317 0.6551 0.3080 0.5904 0.7688 0.2174 0.4294 0.8774 0.3330 0.6635 0.2581 0.9678 0.5832 0.9380 0.0689 0.1080 0.3615 0.1752 0.9369 0.7941 0.9837 0.2153 0.0956 0.5375 0.6504 0.6525 0.4916 0.4062 0.4640 0.3774 0.1426 0.2489 0.0271 0.6749 0.2760 0.9112 0.9862 0.2613 0.0224 0.3852 0.8678 0.0309 0.0715 0.5662 0.1792 0.2675 0.8429 0.0210 0.9328 0.0579 0.6163 0.4985 0.1558 0.0611
S = M(sub2ind([10 10],A,B))
S = 9×1
0.1705 0.5475 0.0369 0.2370 0.3741 0.0057 0.3845 0.1554 0.8151

Más respuestas (1)

Benjamin Kraus
Benjamin Kraus el 24 de En. de 2023
Editada: Benjamin Kraus el 24 de En. de 2023
Is meshgrid or ndgrid what you are trying to do?
[X,Y] = meshgrid([4 5 3],[4 5 3])
X = 3×3
4 5 3 4 5 3 4 5 3
Y = 3×3
4 4 4 5 5 5 3 3 3
[X,Y] = ndgrid([4 5 3],[4 5 3])
X = 3×3
4 4 4 5 5 5 3 3 3
Y = 3×3
4 5 3 4 5 3 4 5 3
After calling either meshgrid or ndgrid you can then convert those matrices into vectors, although that isn't needed.
[X,Y] = meshgrid([4 5 3],[4 5 3]);
X = X(:);
Y = Y(:);
[X Y]
ans = 9×2
4 4 4 5 4 3 5 4 5 5 5 3 3 4 3 5 3 3
Both meshgrid and ndgrid do the same thing, but they swap the orientation of the first two dimensions. meshgrid works with "X" (the first input/output) along the columns and "Y" (the second input/output) along the rows. ndgrid works with "X" (the first input/output) along the rows, and "Y" (the second input/output) along the columns. It may help to understand the behavior of meshgrid and ndgrid (and the difference), if you use different vectors for the inputs, so for the sake of comparing the two:
[X,Y] = meshgrid(1:3,4:6)
X = 3×3
1 2 3 1 2 3 1 2 3
Y = 3×3
4 4 4 5 5 5 6 6 6
[X,Y] = ndgrid(1:3,4:6)
X = 3×3
1 1 1 2 2 2 3 3 3
Y = 3×3
4 5 6 4 5 6 4 5 6
Also, if the first two inputs are the same, you can use a shortcut and just provide one input:
[X,Y] = meshgrid([4 5 3])
X = 3×3
4 5 3 4 5 3 4 5 3
Y = 3×3
4 4 4 5 5 5 3 3 3
[X,Y] = ndgrid([4 5 3])
X = 3×3
4 4 4 5 5 5 3 3 3
Y = 3×3
4 5 3 4 5 3 4 5 3

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de En. de 2023

Comentada:

el 24 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by