Reshape a matrix based on indices placed on another matrix

11 visualizaciones (últimos 30 días)
federico nutarelli
federico nutarelli el 22 de Jul. de 2022
Respondida: Voss el 22 de Jul. de 2022
Hi all,
I have a matrix called combinations and a vector whose dimension in 119x119.
The matrix combinations basically includes all the combinations of rows and columns of the desired matrix and the vector collects the values that I would like to be input. To make a practical example with a 9x9 matrix, say combination is made like this:
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
...
1 9
2 9
3 9
4 9
5 9
6 9
7 9
8 9
9 9
and vector of 81 elemnts is made like this:
NaN
0.78
0.34
...
0.56
...
NaN
where NaN is the 81th element and 0.56 is the element in correspondence of the couple 2-1 in the combination matrix. Notice that the elements of the main diagonal would be all NaNs.
Now what I would like to obtain is the 9x9 matrix with the values in the vector:
NaN 0.56 ...0.88
0.78 NaN ...0.99
...
0.34 ... NaN
Thank you

Respuesta aceptada

Voss
Voss el 22 de Jul. de 2022
v = rand(81,1); % something like
v(1:10:end) = NaN; % your 81-by-1 vector
reshape(v,9,[])
ans = 9×9
NaN 0.4622 0.7474 0.1754 0.6380 0.7944 0.1442 0.0318 0.6000 0.5074 NaN 0.1426 0.9652 0.0757 0.5693 0.2202 0.9093 0.0783 0.2977 0.4874 NaN 0.7450 0.9238 0.8236 0.2739 0.6859 0.7228 0.4736 0.4040 0.6243 NaN 0.4468 0.9677 0.2733 0.6286 0.5913 0.8471 0.4506 0.6928 0.3567 NaN 0.8600 0.9703 0.4879 0.0925 0.1628 0.1126 0.7963 0.1397 0.9743 NaN 0.8280 0.7346 0.2311 0.0919 0.1964 0.4984 0.3028 0.5550 0.7968 NaN 0.1212 0.8937 0.9366 0.3644 0.5989 0.2283 0.4071 0.9791 0.0451 NaN 0.1535 0.9691 0.9074 0.4224 0.5206 0.1697 0.4725 0.8304 0.1909 NaN

Más respuestas (0)

Categorías

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

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by