Randomly change certain characters of a character matrix

조회 수: 1 (최근 30일)
Nick DeMarchis
Nick DeMarchis 2019년 5월 21일
편집: Adam 2019년 5월 21일
Hi there! I have a very large (~300,000 by 16) character vector. How could I insert a section that randomly changes, say 1000 of the characters (not lines) to some other character that I define? Thank you!

채택된 답변

Adam
Adam 2019년 5월 21일
편집: Adam 2019년 5월 21일
If you mean basic letters, then e.g.
idx = randi( numel( yourCharArray ), 1000, 1 );
replacementChars = char( randi( 26, 1000, 1 ) + 96 );
yourCharArray( idx ) = replacementChars;
would replace them with lower case letters from 'a' to 'z'.
If you want other characters then you can change the hard-coded 26 and 96 there as appropriate to index into the ascii character set. 26 obviously represents the 26 letters of the alphabet and 'a' is represented by ascii 97. The random integers will be created from 1 to 26 to add to this.
If you want to you can be fussier to ensure that all your 1000 indices are unique also, but I shall leave that for you.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by