Generate a cell with 10 elements

2 次查看(过去 30 天)
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 个评论
Jan
Jan 2014-10-24
编辑:Jan 2014-10-24
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.

请先登录,再进行评论。

采纳的回答

Mohammad Abouali
Mohammad Abouali 2014-10-24
编辑:Mohammad Abouali 2014-10-25
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 个评论
Silviya
Silviya 2014-10-25
Thanks a lot, Mohammad!Very clear explanation, indeed, I got it with no problem!!

请先登录,再进行评论。

更多回答(2 个)

Andrei Bobrov
Andrei Bobrov 2014-10-25
编辑:Andrei Bobrov 2014-10-25
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 2014-10-25
编辑:Image Analyst 2014-10-25
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by