随机填充矩阵,无行重复 [英] Fill matrix randomly without row-repetitions

查看:77
本文介绍了随机填充矩阵,无行重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助.我是Matlab脚本的新手,需要一些帮助.我有一系列数字:

Please help. I'm new to matlab scripting and need a bit of help. I have a series of numbers:

A = [1 1 1 2 2 2 3 3 3 4 4 4 5]

A=[1 1 1 2 2 2 3 3 3 4 4 4 5]

我想将其随机填充到8x12矩阵中,而在同一行中没有相同的数字.最后,我希望8x12矩阵的所有空"单元格都用0或nan填充.

which I want to fill randomly into an 8x12 matrix without having the same number in the same row. At the end I want all the "empty" cells of the 8x12 matrix being filled with 0's or nan.

一个例子可能是:

result =

3 1 5 2 4 5 0 0 0 0 0 0

3 1 5 2 4 5 0 0 0 0 0 0

4 1 3 2 0 0 0 0 0 0 0 0

4 1 3 2 0 0 0 0 0 0 0 0

1 3 4 2 0 0 0 0 0 0 0 0

1 3 4 2 0 0 0 0 0 0 0 0

推荐答案

  • 确保对A进行排序. A = sort(A) 更多信息
  • 创建一个空矩阵.
  • 对于A中的每个数字:更多信息
    • 找出数字的重复次数->在A中循环,开始是数字的第一个出现,结束是最后一个,n = last-first + 1
    • 找到所有具有额外数​​字空间的行,只需执行两次for循环并跟踪零元素即可.
    • 随机选择n行-> 更多信息.为此,使所有可用行索引的数组R.然后使用提供的函数在1..size(R,2)之间随机取样并获取所有值,您现在就拥有了行索引.
    • 随机选择每一行中的空白点并分配编号
      • make sure A is sorted. A = sort(A)more info
      • make an empty matrix.
      • For each number in A: more info
        • find out how many repetitions of the number there are -> for loop in A, start is the first occurance of the number, end is the last, n = last-first+1
        • find all rows that have space for an extra number, just do a double for loop and keep track of elements that are zero
        • randomly pick n rows -> more info. To do this, make an array R of all available row indixes. Then take a random sample between 1..size(R,2) with the provided function and get all the values, you now have your row indixes.
        • randomly pick one of the empty spots in each of the selected rows and assign the number
        • 这篇关于随机填充矩阵,无行重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆