[英]how to find the number of repeating strings and their array index in a string array in matlab
I have a long array (1x75000 !) of string data. 我有一个很长的字符串数据数组(1x75000!)。 In this array, there are repeated strings.
在此数组中,有重复的字符串。 i want to find the array indices and the number of each repeating string.
我想找到数组索引和每个重复字符串的数量。 Eg
例如
A=['abc' 'efg' 'hij' 'abc' 'hij' 'efg' 'klm']; A = ['abc''efg''hij''abc''hij''efg''klm']; the answer should be: 2 times 'abc' at array indices 1, 4 2 times 'efg' at array indices 2, 6 2 times 'hij' at array indices 3, 5 1 time 'klm' at array indices 7
答案应该是:数组索引为1的'abc'的2倍,数组索引为2的'efg'的2倍,数组索引为3的'hij'的2倍,数组2的'hij'的2倍,数组索引7的'klm'的1倍
notice the large size of the array (1x75000) 注意阵列的大尺寸(1x75000)
This code should work: 此代码应工作:
<?php
$array = array('abc','wrerwe','wrewer','abc');
$out = array();
foreach ($array as $key => $value) {
if (!isset($out[$value])) {
$out[$value]['nr'] = 0;
$out[$value]['index'] = array();
}
++$out[$value]['nr'] ;
$out[$value]['index'][] = $key;
}
foreach ($out as $k => $v) {
echo "item ".$k." repeats ".$v['nr'].' times at positions: ';
echo implode(', ', $v['index']);
echo "<br />";
}
But so far I haven't tested in on such big array. 但是到目前为止,我还没有在如此大的阵列上进行测试。 In fact I don't think you should operate on such big arrays.
实际上,我认为您不应该在如此大的阵列上运行。 You should rather divide it on smaller arrays.
您应该将其划分为较小的数组。
I've tested it on 75000 array using code ( source for generating random string from How to create a random string using PHP? ) : 我已经使用代码在75000数组上对其进行了测试(来自如何使用PHP创建随机字符串的源代码):
<?php
$array = randomTexts(75000);
$out = array();
foreach ($array as $key => $value) {
if (!isset($out[$value])) {
$out[$value]['nr'] = 0;
$out[$value]['index'] = array();
}
++$out[$value]['nr'] ;
$out[$value]['index'][] = $key;
}
foreach ($out as $k => $v) {
echo "item ".$k." repeats ".$v['nr'].' times at positions: ';
echo implode(', ', $v['index']);
echo "<br />";
}
function randomTexts($nr) {
$out = array();
$validString = 'abddefghihklmnopqrstuvwzyx';
for ($i=0; $i< $nr; ++$i) {
$len = mt_rand(5,10);
$out[] = get_random_string($validString, $len);
}
return $out;
}
function get_random_string($valid_chars, $length)
{
// start with an empty random string
$random_string = "";
// count the number of chars in the valid chars string so we know how many choices we have
$num_valid_chars = strlen($valid_chars);
// repeat the steps until we've created a string of the right length
for ($i = 0; $i < $length; $i++)
{
// pick a random number from 1 up to the number of valid chars
$random_pick = mt_rand(1, $num_valid_chars);
// take the random character out of the string of valid chars
// subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1
$random_char = $valid_chars[$random_pick-1];
// add the randomly-chosen char onto the end of our string so far
$random_string .= $random_char;
}
// return our finished random string
return $random_string;
}
It also seems to work but it takes a few seconds 它似乎也可以工作,但是要花几秒钟
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.