deriving new variable from existing column vector using for-loop
Mostrar comentarios más antiguos
Hello Matlab community!
I am a newbie matlab user and trying to be comfortable in using for-loops to derive a new variable.
In my example code below. I designated variable 'c' as my column vector of zeros and ones. I wish to run a for loop for that variable and create a new variable 'data_new' with the following conditions:
** if the value of c is 1 then assign a random variable between 5 to 29 else 0.
**it seems to create those zeros but failed to assign a random variable for the if condition that I stated. here's the error message that I encountered: Unable to perform assignment because the left and right sides have a different number of elements.
a=ones(1,4);
b=zeros(1,4);
c=[a b]'; %column vector 8x1
existing_data=c
for i=1:length(c)
if (existing_data(i)==0)
data_new(i)=randi([5,29],8,1)' %% i wish to generate the same size column vector where the ones == randomly assigned data bet 5-29
else
data_new(i)=0
end
end
Thank you!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!