mercredi 5 octobre 2016

A for/while/if program to create a vector in matlab

My task is to write a program to create a vector which has its first 10 elements to be 10, element 11-20 to be 20, element 21-30 to be 30, element 31-40 to be 40, & element 41-50 to be 50.

This is how i did it.

v=zeros(1,50); for i = 1:10 v(i)=10; end for i =11:20 v(i)=20; end for i = 21:30 v(i)=30; end for i = 31:40 v(i)=40; end for i = 41:50 v(i)=50 end

How can I write this more efficiently? (Im new to matlab and programming in general. I'm thinking about how to construct this using while or if loops instead but without being able to produce a more efficient code.)

Aucun commentaire:

Enregistrer un commentaire