Filter löschen
Filter löschen

Fill in a matrix

1 Ansicht (letzte 30 Tage)
Konstantinos
Konstantinos am 10 Jan. 2015
Kommentiert: Image Analyst am 10 Jan. 2015
I want to create a row matrix of 16 elements and fill in the first 8 elements (as well as the last 8 elements) of it, with integers from 1 to 8.
i.e. result = ( 2 5 2 8 4 1 4 7 3 5 3 7 8 4 6 6 )
Any help could be useful. thanks in advance!
  1 Kommentar
Azzi Abdelmalek
Azzi Abdelmalek am 10 Jan. 2015
You can just ask to create a matrix with 16 elements taken randomly from 1 to 8

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 10 Jan. 2015
This seems to do what you want:
result = randi([1 8], 1, 16)
producing:
result =
4 1 3 3 3 2 3 1 8 1 8 4 1 3 6 7
  1 Kommentar
Image Analyst
Image Analyst am 10 Jan. 2015
Just a fine point, this gives doubles that have integer values. If you really want an integer data type, cast to an integer data type such as int32:
result = int32(randi([1 8], 1, 16))
whos result % Prove that it's an integer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 10 Jan. 2015
randi(8,1,16)

Community Treasure Hunt

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

Start Hunting!

Translated by