필터 지우기
필터 지우기

Matrix dimensions must agree problem

조회 수: 3 (최근 30일)
Gokhan Kayan
Gokhan Kayan 2018년 2월 5일
댓글: Gokhan Kayan 2018년 2월 5일
I have an cell array (7879*1) and the first 6 six terms are:
LV.cc-LV.vr.cr
CL.ptp-LV.cr.ca
LP.li-CL.ptp
LV.cc-LV.vr.cr
CL.ptp-LV.cr.ca
LP.li.ca-RG.le.ca
SC.gl
CM.vr-RG.ca
I want to write a code that if cell is equal to LV.cc-LV.vr.cr then B is equal to A, and if cell is equal to SC.gl then B is equal to E. I write this code but it gives me error "Matrix dimensions must agree problem". My code is:
for i=1:numel(cell);
if cell{i}=='LV.cc-LV.vr.cr';
B{i}='A';
else if cell{i}=='CL.ptp-LV.cr.ca';
B{i}='B';
else if cell{i}=='LP.li-CL.ptp';
B{i}='C';
else if cell{i}=='LP.li.ca-RG.le.ca';
B{i}='D';
else if cell{i}=='SC.gl';
B{i}='E';
else cell{i}=='CM.vr-RG.ca';
B{i}='F';
How can I solve Matrix dimensions must agree problem ? Thank you.

채택된 답변

Guillaume
Guillaume 2018년 2월 5일
You use strcmp to compare char arrays not ==.
Also note that there is a big difference between elseif and else if. The former continues the previous if, the latter starts a new if within the else portion of the previous if requiring an additional end.
Also, note that calling your cell array cell is a very bad idea, since you won't be able to create other cell arrays with cell function, now shadowed by your variable
So:
if strcmp(carray{i}, 'LV.cc-LV.vr.cr')
B{i}='A';
elseif strcmp(carray{i}, 'CL.ptp-LV.cr.ca')
B{i}='B';
%...
However, an even simpler method is to use ismember:
B = cell(size(carray)); %using the cell function which can't be used if your cell array is called cell!
replacements = {'A', 'B', 'C', 'D', 'E', 'F'};
[found, where] = ismember(carray, {'LV.cc-LV.vr.cr', 'CL.ptp-LV.cr.ca', 'LP.li-CL.ptp', 'LP.li.ca-RG.le.ca', 'SC.gl', 'CM.vr-RG.ca'});
B(found) = replacements(where(found))
  댓글 수: 1
Gokhan Kayan
Gokhan Kayan 2018년 2월 5일
Thank you so much Guillaume :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by