how to output selectively a random matrix

3 ビュー (過去 30 日間)
Brian Kim
Brian Kim 2017 年 9 月 18 日
コメント済み: Brian Kim 2017 年 9 月 21 日
i have code. I'll explain this and then ask you a question.
function numcase_ewn=Numcase_MW(ewn,col_l)
for i1=1:1:10
for i2=1:70000
if sumA==sum_te && A(1)==tmp_ewn(1)
final=[final A];
--------- 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.)))

回答 (1 件)

Cam Salzberger
Cam Salzberger 2017 年 9 月 18 日
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.
No need for this, as all values within "tmp_ewn" were "fixed" already.
is the same as:
randi([4 12], col_l, 1)
and using randi tends to be more readable.
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.
is the same as:
BBB = final;
so why not just use "final"?
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.
  3 件のコメント
Brian Kim
Brian Kim 2017 年 9 月 21 日
thank you so much :) I was so inspired by your answers.



Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!