how do I replace zeros

5 visualizzazioni (ultimi 30 giorni)
Aleksy Golemo
Aleksy Golemo il 7 Feb 2018
Risposto: Star Strider il 7 Feb 2018
  • x = zeros(1,17); % this is my initial vector of zeros
  • x = round(200 + (400-200).*rand(1,16)); % this is my generation of random numbers between 200 and 400
  • sum(x); % this is my sum of my randomly generated numbers
How do I replace the zeros in my initial vector with the 1x16 vector that I generated, and and it with the sum, creating a final 1x17 vector?

Risposte (1)

Star Strider
Star Strider il 7 Feb 2018
Address 16 of the elements of ‘x’:
x = zeros(1,17); % this is my initial vector of zeros
x(1:16) = round(200 + (400-200).*rand(1,16)); % this is my generation of random numbers between 200 and 400
sx = sum(x); % this is my sum of my randomly generated numbers
See the documentaiton on Matrix Indexing (link) for details.

Community Treasure Hunt

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

Start Hunting!

Translated by