# Quickly create a vector of ones and zeros

508 visualizzazioni (ultimi 30 giorni)
Raldi il 5 Mag 2014
Risposto: Khayalvili Ramu il 18 Mar 2020
My question is if there is a way to create a vector of zeros
vec =
0 0 0 0 0 0 0 0
and specify a position lets say from the second until the fourth element to be ones.
vec =
0 1 1 1 0 0 0 0
Is there a quick way of doing this in Matlab avoiding loops?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Youssef Khmou il 5 Mag 2014
vectorization is possible :
N=10;
vec=zeros(N,1);
positions=[2:4];
vec(positions)=1;
##### 4 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Jos (10584) il 27 Feb 2019
positions = 2:4;
would do, so the square brackets ARE superfluous ...
Innocent Okoloko il 27 Feb 2019
You are right. Indeed x=zeros(1,21) will give the same result, makingthe code even shorter and more efficient. I had previously encountered a situation that made me adopt the sqaure brackets and I tried to ransack through my codes, not enough time to do all that now. I guess it is just useful for contantenation e.g.
num=ones(1,5);
num=[num zeros(1,10)]

Accedi per commentare.

### Più risposte (4)

Matt J il 5 Mag 2014
Modificato: Matt J il 5 Mag 2014
N=10;
positions=[2:4];
vec=sparse(1,positions,1,1,N);
and then optionally, if you want the vector in full form,
vec=full(vec),
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Innocent Okoloko il 26 Feb 2019
Faster coding
x=[zeros(1,10)];
x(2:4)=1
##### 2 CommentiMostra NessunoNascondi Nessuno
Stephen23 il 26 Feb 2019
Note that the square brackets are superfluous, and likely slow down the code:
Jos (10584) il 27 Feb 2019
x = zeros(1,10)
will give the same result as your code, so the square brackets ARE superfluous ...

Accedi per commentare.

Jos (10584) il 26 Feb 2019
N = 10 % final length of NewVec
pos1 = 3 % start index of the 1's
n1 = 4 % number of 1's
% one-liner. NewVec should not exist yet
NewVec([N pos1:pos1+n1-1]) = [0 ones(1,n1)]
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Khayalvili Ramu il 18 Mar 2020
close all
clc
K=[1 2 3 6];
P=[1 2 2 2];
S=zeros(1,8);
T=zeros(1,length(S));
for i=0:(length(S)-1)
S(1,i+1)=i;
for k=0:length(K)-1
T(1,k+1)=K(1,k+1);
for l=length(K):length(T)-1
T(1,l+1)=T(1,l-3);
end
end
end
S_temp=zeros(length(S),length(S));
j=0;
for i=0:length(S)-1
j=j+S(1,i+1)+T(1,i+1);
j=mod(j,8);
S([i+1 j+1])=S([j+1 i+1]);
S_temp(i+1,:)=S;
end
KS=zeros(1,length(P));
j=0;
for i=0:length(P)-1
i_temp=mod(i+1,8);
j=mod(j+S(1,i_temp+1),8);
S([i_temp+1 j+1])=S([j+1 i_temp+1]);
t=mod(S(1,i_temp+1)+S(1,j+1),8);
ks=S(1,t+1);
KS(:,i_temp)=ks;
C=bitxor(KS,P);
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!