how to output selectively a random matrix

3 views (last 30 days)
Brian Kim
Brian Kim on 18 Sep 2017
Commented: Brian Kim on 21 Sep 2017
i have code. I'll explain this and then ask you a question.
function numcase_ewn=Numcase_MW(ewn,col_l)
tmp_ewn=fix(ewn);
sum_te=sum(tmp_ewn);
sum_te=fix(sum_te);
final=[];
for i1=1:1:10
for i2=1:70000
A=sort(round(8*rand(col_l,1)+4));
sumA=sum(A);
if sumA==sum_te && A(1)==tmp_ewn(1)
final=[final A];
end
end
BBB=final(:,:);
sum(BBB);
BBB=unique(BBB','rows');
numcase_ewn=BBB';
end
--------- Input ---------
'ewn' is the matrix I know.
'col_l' is col's length.
------------------------
In conclusion, this function places the matrices for the same sum into separate rows.
For example,
if ewn=[4 4 4 4 5], col_l=3,
'numcase_ewn' is
numcase_ewn = [ 4 4 4 4
5 6 7 8
12 11 10 9]
by the way, I use this function to write the main code
I thought this function is so slow.
Therefore, i want to revise this.
((((P.S. If possible, let me know if there is a way to get a matrix that only gets one case, instead of getting a set of all cases matrices.)))

Answers (1)

Cam Salzberger
Cam Salzberger on 18 Sep 2017
Hello Brian,
From both your question and your code, I cannot understand what you are trying to get out of this. What is "numcase_ewn" in comparison to "ewn"? For that matter, what do you mean by "case" in regards to numbers? Are you just looking for an example matrix that has the same sum as "ewn" and starts with the same number, but is somewhat randomly generated otherwise? Because that could probably be done in 3 lines or so.
I can give you advice on specific issues I see with your code, even if I don't fully understand what you are trying to do with it.
sum_te=fix(sum_te);
No need for this, as all values within "tmp_ewn" were "fixed" already.
round(8*rand(col_l,1)+4)
is the same as:
randi([4 12], col_l, 1)
and using randi tends to be more readable.
sumA=sum(A);
If a value is not going to be used more than once, generally no need to make a separate variable to store it, at least if the calculation to complete it is simple.
final=[final A];
This could potentially be a large time-sink. You are growing the size of an array in a loop, which requires allocating new memory blocks nearly every time. Even if you don't know the size of the output array, I'd still recommend preallocation unless you can make a strong argument against it. Pay attention to the code analyzer warnings.
BBB=final(:,:);
is the same as:
BBB = final;
so why not just use "final"?
sum(BBB);
doesn't assign its output to anything, so you can get rid of it.
The biggest time-sink though is that you have two loops going on, but you are using the indices for nothing, and you may not need to be looping like that. As far as I can tell, the loop limits were chosen randomly to ensure that you "tried a lot to find an answer", but they don't guarantee that you have an answer, or that you have only one answer.
If by "get a matrix that only gets one case, instead of getting a set of all cases matrices", you mean that you want only one of these arrays that corresponds to "ewn", rather than constantly concatenating on new ones that fit the criteria, then a while loop is probably the way to go. Just loop while you haven't yet found a matrix that fits the criteria. That will "guarantee" exactly one answer, though it may still take a long time since this algorithm has Bogosort-level efficiency.
-Cam
  3 Comments
Brian Kim
Brian Kim on 21 Sep 2017
thank you so much :) I was so inspired by your answers.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!