# Matrix dimensions must agree problem

2 vues (au cours des 30 derniers jours)
Gokhan Kayan le 5 Fév 2018
Commenté : Gokhan Kayan le 5 Fév 2018
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.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Guillaume le 5 Fév 2018
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 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Gokhan Kayan le 5 Fév 2018
Thank you so much Guillaume :)

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Logical dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by