How to solve "Matrix dimensions must agree" getting from Matlab.

조회 수: 1 (최근 30일)
Mary
Mary 2019년 8월 13일
편집: dpb 2019년 8월 14일
Hello experts,
I tried to run this code to calculate the mean value and std for specific condition, and the vaible listed in my workspace are
difficult 288x1 cell
percorr 288x1 double
sleepstate 288x1 cell
for difficult =1:288
for sleepstate =1:288
for percorr =1:288
if ('difficult = 0-back') & ('sleepstate = SD')
Mzeroback = mean(percorr)
else
Mzeroback = 0 ;
end
end
end
end
after running this code selection, Ihave this error
Matrix dimensions must agree.
What I could do to fix this issues?
Thanks inadvance for any help;
M

답변 (2개)

dpb
dpb 2019년 8월 13일
if ('difficult = 0-back') & ('sleepstate = SD')
is bad syntax on many fronts. First you've built a quoted string by using ' around the expressions instead of using the variables and then '=' is the assignment operator, not equality which is '==' That aside, in ML since difficult and sleepstate aren't scalars, if() would only be true if and only if every element inside were to match the desired condition. All that aside, if they are cell arrays, then you need to dereference the cell array elements with {}.
So...having listed at least some of the problems, how to fix... :)
First, presuming the cell arrays are not arrays of cells of additional elements, use a table --
t=table([cell2table(difficult) cell2table(percorr) cell2table(sleepstate)]);
I'm then guessing at least difficult and sleepstate should be categorical variables; possibly percorr as well but it may be numeric; can't tell.
t.difficult=categorical(t.difficult);
t.sleepstate=categorical(t.sleepstate);
then use comparison operations to find the combination of variable state wanted with logical expressions.
  댓글 수: 1
Mary
Mary 2019년 8월 13일
I have a table that 288x4 tab that contains all the varibles I have listed above: percorr contains numbers
sleepstate contains either SD or HD
difficult contains 0-back or 1-back or 2-back or 3-back
sujectnum
the questions herehow to make if else statment to calculate the mean and std for example of each type of sleep state and each type of difficult level let say SD & 0-back sepratly and SD &1-back...etc. I mean for each combinations of sleep state and difficult level.
Thanks,
M

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


dpb
dpb 2019년 8월 14일
편집: dpb 2019년 8월 14일
OK...still a little unclear but I think I've got enough of a picture to at least lay out the general idea.
It'd be a lot easier to demonstrate if we had the data (or a subset of it with a couple levels of the variables) so we knew we were talking same thing in MATLAB; the non-english native language makes it harder to be sure we really mean the same thing in text; code is code.
"percorr contains numbers, sleepstate contains either SD or HD, difficult contains 0-back or 1-back or 2-back or 3-back"
OK, as I presumed, except for percorr these should all be categorical variables rather than cellstr() or char() which will be why had cell arrays above.
By using categorical for the nonummeric categorizing variables levels you have automatically created a useful grouping variable for each variable but to get the combination across groups you use findgroups and then use splitapply to compute over the grouping variables just identified.
I will assume the table is a MATLAB table and has variable name t and then use your variable names...
[g,sleep,diff]=findgroups(t.sleepstate,t.difficult); % group the two variables of interest
sd=splitapply(@std,t.percorr,g); % compute std of percorr by group
res=table(sleep,diff,sd) % collect the output in a table
WARNING! Air code; no data to test so many assumptions made...but the general idea is the way to go. See the following link for more details and examples: GGrouping-variables-for-splitting-data

카테고리

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