Outputing Array from Nested Why Loops

Hi, The purpose of my code is to save for a number n, say 3, the following in an array A:
A(1) = randi(3)
A(2) = randi(2)
A(3) = randi(1)
A(4) = randi(3)
A(5) = randi(2)
....
I have written the following code, but right now it is not doing what I want - how can I change that?
function [ArrayFinal] = RandomizeTrials(numberofSeq, lengthofArray)
ArrayFinal = zeros(1,lengthofArray)
i = 1;
while i < lengthofArray+1
while i < numberofSeq+1
Array(i) = randi(numberofSeq);
numberofSeq = numberofSeq-1;
i = i + 1;
end
i+3;
end
end

1 件のコメント

Stephen23
Stephen23 2015 年 4 月 29 日
Note that you should avoid using i and j for loop variable names, as these are both names of the inbuilt imaginary unit.

回答 (3 件)

the cyclist
the cyclist 2015 年 4 月 28 日
編集済み: James Tursa 2015 年 4 月 29 日

1 投票

There were lots of little issues with your code. I hope this was not homework, because I tried to pretty much fix them all:
function [Array] = RandomizeTrials(numberofSeq, lengthofArray)
Array = zeros(1,lengthofArray);
i = 1;
while i < lengthofArray+1
n=1;
while n < numberofSeq+1
Array(i) = randi(numberofSeq+1-n);
n = n + 1;
i = i + 1;
end
i+numberofSeq;
end
end

8 件のコメント

MiauMiau
MiauMiau 2015 年 4 月 29 日
編集済み: MiauMiau 2015 年 4 月 29 日
No, I was doing them for myself. Thank you, I will check what I did wrong! Why exactly is
numberofSeq = numberofSeq-1;
in the while loop wrong?
Søren Jensen
Søren Jensen 2015 年 4 月 29 日
look at my answer and you should get it..
Basically you are changing numberofSeq at every itteration, and if you did not save it in a buffer (like me) or avoid changing it (like the cyclist), the second (third, fourth, fifth...) time you enter your internal loop will be very short (since "n<numberofSeq" already returns "false")
MiauMiau
MiauMiau 2015 年 4 月 29 日
oh so I don't have local variables in Matlab? Ie changing the numberofSeq in the while loop will change it outside of the loop too?
Stephen23
Stephen23 2015 年 4 月 29 日
編集済み: Stephen23 2015 年 4 月 29 日
"local" variables are available everywhere within one workspace. It is quite handy because often the last loop iteration is a special case and the values can be referred to after the loop too.
MiauMiau
MiauMiau 2015 年 4 月 29 日
How comes then when I run:
j = 5;
i = 4;
for i=1:4
A(i) = i*i;
j-1;
end
j
j does not seem to be changed - it is still 5 at the end?
Ilham Hardy
Ilham Hardy 2015 年 4 月 29 日
if you change the code into this:
j = 5;
i = 4;
for i=1:4
A(i) = i*i;
j = j-1;
end
j
j will be 1 at the end of the for loop
MiauMiau
MiauMiau 2015 年 4 月 29 日
got it, thx!
Stephen23
Stephen23 2015 年 4 月 29 日
編集済み: Stephen23 2015 年 4 月 30 日
Because j-1 does not allocate that new values to anything. MATLAB does not have a short-hand for incrementing/decrementing variables, so you need to explicitly assign that value: j = j-1
Søren Jensen
Søren Jensen 2015 年 4 月 28 日

1 投票

A(1) = randi(3)
A(2) = randi(2)
A(3) = randi(1)
A(4) = randi(3)
A(5) = randi(2)
so you have a minimum bound where the argument for randi resets, or is it reset when it hith the floor (1)?
anyways, using i for both loops should overwrite each other and mess up your intention i think. try this:
saved = numberofSeq; % saved for use in the outer itteration i = 1;
while i<(lengthofArray+1)
numberofSeq = saved;
for k=1:(numberofSeq+1)
Array((i-1)*numberofSeq+k) = randi(numberofSeq);
numberofSeq = numberofSeq-1;
end i=i+numberofSeq;
end
% you seem to be adapt in other coding languages, so you can probably guess my intention.. unless i missed yours
Stephen23
Stephen23 2015 年 4 月 29 日
編集済み: Stephen23 2015 年 4 月 30 日

1 投票

It is possible to vectorize this code a bit by using randi to generate vectors instead of many separate scalar values: this will likely be faster generally, and certainly if out_len/num_seq is a large value.
function out = RandomizeTrials(num_seq, out_len)
X = max(2,ceil(out_len/num_seq));
for k = num_seq:-1:1
out(k,:) = randi(num_seq-k+1,1,X);
end
out = out(1:out_len);
end

この質問は閉じられています。

質問済み:

2015 年 4 月 28 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by