# Creating a new matrix and insert certain values into that matrix

30 Ansichten (letzte 30 Tage)
eko supriyadi am 30 Aug. 2021
Kommentiert: eko supriyadi am 2 Sep. 2021
Dear Matlab community
suppose i want make new matrix 4*4 and insert a value based on the row-column position of the matrix, and also if there is no value it is filled with nan.. any ideas to solve it?
the value where i want to insert are:
A(1,1) = 2
A(3,1) = 2
A(3,1) = 2
A(2,3) = 4
A(2,4) = 2
A(3,1) = 3
A(3,2) = 3
A(4,1) = 2
A(3,4) = 1
and give the result:
A =
2 NaN 3 2
NaN NaN 3 NaN
2 4 NaN 1
4 2 NaN NaN
##### 1 KommentarKeine anzeigenKeine ausblenden
Stephen23 am 30 Aug. 2021
Bearbeitet: Stephen23 am 30 Aug. 2021
Note that the indexing and values you show will NOT generate that "result" matrix.
As far as I can tell, half of the values in your results matrix are randomly placed.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 30 Aug. 2021
R = [1,3,3,2,2,3,3,4,3];
C = [1,1,1,3,4,1,2,1,4];
V = [2,2,2,4,2,3,3,2,1];
M = nan(4,4);
M(sub2ind(size(M),R,C)) = V
M = 4×4
2 NaN NaN NaN NaN NaN 4 2 3 3 NaN 1 2 NaN NaN NaN
##### 1 KommentarKeine anzeigenKeine ausblenden
eko supriyadi am 2 Sep. 2021
tks stephen

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!