Generate a cell with 10 elements

2 visualizaciones (últimos 30 días)
Silviya
Silviya el 24 de Oct. de 2014
Comentada: Mohammad Abouali el 25 de Oct. de 2014
Hello,
I have the following assignment:
1.3 Generate a cell with ten elements. Each element is a vector of random length (between 1 and 10) containing ones if the length is odd and zeros if the length is even.
For which I've been given the following solution:
NumofVec = 10;
for 1 = 1:NumofVec;
Vlen = floor(rand*10)+1;
V{i} = ones (1, Vlen)*rem(Vlen,2);
end
Could you please explain to me the meaning of the last two lines?
Vlen = floor(rand*10)+1;
V{i} = ones (1, Vlen)*rem(Vlen,2);
Thanks a lot!
Massive thanks to anyone attempting to help!!
Silvi
  1 comentario
Jan
Jan el 24 de Oct. de 2014
Editada: Jan el 24 de Oct. de 2014
What exactly is your question? Do we need to explain the "=" operator also? Did you read the Getting Started chapters already, such that you are familiar with the cell indexing by the curly braces? Could we post anything better than the help sections for floor, rand, ones and rem?
You for loop contains a typo.

Iniciar sesión para comentar.

Respuesta aceptada

Mohammad Abouali
Mohammad Abouali el 24 de Oct. de 2014
Editada: Mohammad Abouali el 25 de Oct. de 2014
my solution would be:
vlen=randi(10,[10,1])
V=arrayfun(@(x) (mod(x,2)*ones(x,1)),vlen,'UniformOutput',false)
Now the codes that you asked:
Vlen = floor(rand*10)+1;
rand generates a random number between 0 and 1. multiplying by 10 result into a random number between 0 and 10. floor rounds it down so 9.85 becomes 9. so floor(rand*10) generates a number between 0 and 9; hence adding 1 to generate a random number between 1 and 10.
ones (1, Vlen) generates a vector of length Vlen and all elements being one rem(Vlen,2) gives 0 if Vlen is even and gives 1 if Vlen is odd. So that line generates a vector of length Vlen and all the elements would be 1 if Vlen is odd;otherwise, the elements would be 0.
The two line code that I give you at the beginning would do the same thing.
vlen=randi(10,[10,1]) generates 10 random number between 1 and 10.
arrayfun applies (mod(x,2)*ones(x,1)) on each element of Vlen, with x being each element of glen. mod(x,2) again is zero if x is even and it is one if x is odd and ones(x,1) generates a vector of length x.
  2 comentarios
Silviya
Silviya el 25 de Oct. de 2014
Thanks a lot, Mohammad!Very clear explanation, indeed, I got it with no problem!!
Mohammad Abouali
Mohammad Abouali el 25 de Oct. de 2014
you are welcome

Iniciar sesión para comentar.

Más respuestas (2)

Andrei Bobrov
Andrei Bobrov el 25 de Oct. de 2014
Editada: Andrei Bobrov el 25 de Oct. de 2014
V = arrayfun(@(x)rem(x,2)*ones(x,1),randi([1 10],10,1),'un',0);
Hi Silviya! For clarity, please read about MATLAB-functions arrayfun, rem, randi.

Image Analyst
Image Analyst el 25 de Oct. de 2014
Editada: Image Analyst el 25 de Oct. de 2014
I think Mohammad explained it pretty well. Here is the same code in a somewhat easier-to-understand style.
numberOfCells = 10;
V = cell(numberOfCells, 1);
for row = 1: numberOfCells
% Get a random number between 1 and 10 inclusive.
vectorLength = randi(10, 1)
% See if the length is odd or oven.
if rem(vectorLength,2) == 0 % Remainder is 0 so must be even.
% The length is an even number.
% Set V equal to a vector of all zeros.
V{row} = zeros(1, vectorLength);
else
% The length is an odd number.
% Set V equal to a vector of all ones.
V{row} = ones (1, vectorLength);
end
end
See the FAQ for a good intuitive understanding of how cell arrays work: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by