# Why do I get "Index exceeds matrix dimensions"?

조회 수: 1(최근 30일)
Blake 2017년 10월 19일
편집: Cedric Wannaz 2017년 10월 19일
I'm trying to create a matrix of two letter combinations from a string x. A(1,1) = # of aa's, A(1,2) = # of ab's etc. to A(26,26) = # of zz's. The text is all lower case and I thought I solved it, but I keep getting this error message. Ideas as to why? How would I fix / avoid this in the future?
alphabet = 'a':'z';
A = zeros(26);
for j = alphabet
for i = alphabet
y = strcat(alphabet(i),alphabet(j));
A(i,j) = length(strfind(x,y));
end
end

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

### 채택된 답변

Cedric Wannaz 2017년 10월 19일
편집: Cedric Wannaz 2017년 10월 19일
Numeric arrays cannot store arrays of two characters. Try with a cell array. Also, don't index arrays with characters but with numeric indices
alphabet = 'a' : 'z' ;
A = cell( 26, 26 ) ;
for j = 1 : length( alphabet )
for i = 1 : length( alphabet )
A{i,j} = [alphabet(i),alphabet(j)] ; % Concatenation of two letters.
end
end

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

### 범주

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by