Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在matlab中求字符串数组中重复字符串的个数及其数组索引_Arrays_String_Sorting - Fatal编程技术网

Arrays 如何在matlab中求字符串数组中重复字符串的个数及其数组索引

Arrays 如何在matlab中求字符串数组中重复字符串的个数及其数组索引,arrays,string,sorting,Arrays,String,Sorting,我有一个长阵列1x75000!字符串数据的类型。 在这个数组中,有重复的字符串。 我想找到数组索引和每个重复字符串的数量。 例如 A=['abc''efg''hij''abc''hij''efg''klm']; 答案应该是: 数组索引1,4处的2倍“abc” 数组索引2,6处的2倍“efg” 阵列索引3,5处的2次“hij” 阵列索引7处的1次“klm” 请注意1x75000阵列的大尺寸此代码应该可以工作: <?php $array = array('abc','wrerwe','wre

我有一个长阵列1x75000!字符串数据的类型。 在这个数组中,有重复的字符串。 我想找到数组索引和每个重复字符串的数量。 例如

A=['abc''efg''hij''abc''hij''efg''klm']; 答案应该是: 数组索引1,4处的2倍“abc” 数组索引2,6处的2倍“efg” 阵列索引3,5处的2次“hij” 阵列索引7处的1次“klm”

请注意1x75000阵列的大尺寸

此代码应该可以工作:

<?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 />";
}
但到目前为止,我还没有在这么大的阵列上进行测试。事实上,我认为你不应该在这么大的阵列上操作。你应该把它分成更小的数组

我已经在75000阵列上测试了它,使用以下代码源生成随机字符串:

它似乎也能工作,但需要几秒钟

<?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;
}