How to create a vector in Matlab

1 -> I need to create a vector in Matlab that goes from 1 to 500 and has 0.00462962962962962962962962962963 of interval between each element. How do i do that?
2 -> I need to insert tree zeros between every 5 positions of a vector. For example:
v = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...]
I need:
t = [1 2 3 4 5 0 0 0 6 7 8 9 10 0 0 0 11 12 13 14 15...]
Thanks in advance

Respuestas (1)

DGM
DGM el 12 de Abr. de 2021
Editada: DGM el 12 de Abr. de 2021
For the first part:
x=1:1/216:500;
for the second part, what exactly do you need the spaces for? The extra spaces don't actually do anything. Are you trying to create a numeric vector with spaces, or is this for display only?

7 comentarios

Jórdan Venâncio Leite
Jórdan Venâncio Leite el 12 de Abr. de 2021
Editada: Jórdan Venâncio Leite el 12 de Abr. de 2021
I only need to separate the results with zeros.
Sorry, but I didn’t explain the question properly. I have already edited it in the correct way.
Thanks for your answer
DGM
DGM el 13 de Abr. de 2021
Editada: DGM el 13 de Abr. de 2021
If the blocks only need to be zeros:
clf
x = 1:23
n = 5; % number of elements btw zero-blocks
nz = 3; % number of zeros per block
nblocks = floor(numel(x)/n);
y = zeros([1 numel(x)+nblocks*nz]);
xx=1:numel(y);
width=n+nz;
idx = mod((xx-1),width) <= (n-1);
y(idx) = x;
y
or more generally for any predefined block:
clf
x = 1:23
n = 5; % number of elements btw zero-blocks
bk = [100 100 100]; % block to insert
nblocks = floor(numel(x)/n);
y = zeros([1 numel(x)+nblocks*numel(bk)]);
xx=1:numel(y);
width=n+numel(bk);
idx = mod((xx-1),width) <= (n-1);
y(idx) = x;
y(~idx) = repmat(bk,[1 nblocks]);
y
Jórdan Venâncio Leite
Jórdan Venâncio Leite el 13 de Abr. de 2021
Editada: Jórdan Venâncio Leite el 13 de Abr. de 2021
Thanks for your answer. I really didn't understand how I'm going to include the zeros in this graph of mine. It has 108000 values on the bottom axis. I have to include 1080 zeros on the bottom axis for every 1080 values.
DGM
DGM el 13 de Abr. de 2021
I don't really know either. If you need a 1:1 ratio of data and zeros, then inserting 3 for every 5 elements isn't going to achieve that. The above methods can be adjusted to insert a string of N zeros every N elements. That would give you 1:1, but I don't know what N you would want or why.
I don't know if you intend to insert those zeros or replace data with zeros, and I don't know what the motivation is either.
Jórdan Venâncio Leite
Jórdan Venâncio Leite el 13 de Abr. de 2021
Editada: Jórdan Venâncio Leite el 13 de Abr. de 2021
What i need is to show that for every 1080 elements i have a separation always the same size (of 1080), thus separating all the data and, consequently, doubling the bottom axis.
DGM
DGM el 13 de Abr. de 2021
Then set n=1080 and nz=1080, run that on your y-data.
For the x-data, the timesteps are all identical, so you'll have to decide whether you want to extend the timebase or make the timestep shorter.
You'll have to decide what the relationship between x and y is going to be if you insert a bunch of artificial samples into the dataset.
Jórdan Venâncio Leite
Jórdan Venâncio Leite el 13 de Abr. de 2021
Thanks for your answer DGM! I will try it!!

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 12 de Abr. de 2021

Comentada:

el 13 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by