Filter löschen
Filter löschen

Matrix dimensions must agree problem

3 Ansichten (letzte 30 Tage)
Gokhan Kayan
Gokhan Kayan am 5 Feb. 2018
Kommentiert: Gokhan Kayan am 5 Feb. 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.

Akzeptierte Antwort

Guillaume
Guillaume am 5 Feb. 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))

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by