필터 지우기
필터 지우기

Replacing a whole row of a matrix with specific characters

조회 수: 1 (최근 30일)
UET Hussain
UET Hussain 2020년 4월 14일
댓글: UET Hussain 2020년 4월 14일
Hi all,
I have a big matrix, 432000 * 5 (char type). the elements of the matrix are binary (0s and 1s). I want to replace each row with a specific character (e.g. if row is '1 0 1 1 1', it should be replaced by "X"), whereever it appears in the whole matrix.
I am trying the following code but it is giving only 1*32 cell output (1 row, 32 columns and only last column is replaced/updated).
actual matrix (the 432000*5 one) is dependent on my input, which is an image, and it can vary.
I am expecting 432000 characters (either in rows or columns), i.e. each row of original matrix is replaced with corresponding allocated character.
for j=1:len; %len is 432000 in this case, it could vary depending on my input
result = strrep(R(j,:),{'00000','00001','00010','00011','00100','00101','00110','00111','01000','01001','01010','01011','01100','01101','01110','01111','10000','10001','10010','10011','10100','10101','10110','10111','11000','11001','11010','11011','11100','11101','11110','11111'},{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5'});
end

답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 4월 14일
a = char(randi([0 1],4000,5)+'0'); % create a n by 5 char array
b = bin2dec(a); % convert it to decimal
b(b>25) = b(b>25) - 'A' + '0' - 26; % correct for poisition of numbers
c = char(b + 'A'); % convert back to char
  댓글 수: 1
UET Hussain
UET Hussain 2020년 4월 14일
could not get the point.
the point is, i wanted to replace each row by a new character. whereever a specific pattern (5 bits) appears, it should be replaced by corresponding character. e.g. if 0 0 0 0 0 appears, it should be replaced by A etc.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by