Generate a cell with 10 elements

2 ビュー (過去 30 日間)
Silviya
Silviya 2014 年 10 月 24 日
コメント済み: Mohammad Abouali 2014 年 10 月 25 日
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!!
Mohammad Abouali
Mohammad Abouali 2014 年 10 月 25 日
you are welcome

サインインしてコメントする。

その他の回答 (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.
  1 件のコメント
Silviya
Silviya 2014 年 10 月 25 日
Thanks!

サインインしてコメントする。


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
  1 件のコメント
Silviya
Silviya 2014 年 10 月 25 日
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by