# anova1: Operands to the || and && operators must be convertible to logical scalar values.

조회 수: 3(최근 30일)
William Harris 2021년 2월 16일
댓글: Jon 2021년 2월 19일
I'm analysing some MRI data to assess signal drift (decay with prolonged scanning) and I'm trying to run an anova on grouped data to see if there is a significant difference between different blocks of timepoints. However, when I run the anova, I get the following error:
"Operands to the || and && operators must be convertible to logical scalar values.
Error in anova1 (line 48)
if (nargin>0 && strcmp(x,'kruskalwallis'))"
My code is as follows (R1rho is a variable containing the mean value for each image acquisition):
block1 = R1rho(1:49)
block3 = R1rho(100:149)
block4 = R1rho(150:199)
block5 = R1rho(200:249)
block6 = R1rho(250:299)
block7 = R1rho(300:349)
driftGrouping = {'1:49', '50:99', '100:149', '150:199', '200:249', '250:299'};
R1rhoANOVA = [{block1}, {block3}, {block4}, {block5}, {block6}, {block7}];
anova1(R1rhoANOVA, driftGrouping);

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

### 채택된 답변

Jon 2021년 2월 16일
I don't think you have defined your grouping variable, second argument to anova1 correctly.
According to the documentation it must be one of
numeric vector | logical vector | categorical vector | character array | string array | cell array of character vectors
It looks like yours is a cell array containing numeric arrays
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
Jon 2021년 2월 19일
I looked into this a little more. Actually anova1 does not throw an error when supplied with a grouping variable as long as the first argument is either a vector or matrix (which is the only kind of input it expects) . You were entering a cell array which is not an input type it expects. It then has problems with the string comparison. Would be nicer though if it just told you that it did not accept a cell array for the first input.
Regarding why you might have used a cell array. I see that in your example all of the blocks have 50 elements except for block1 that only has 49. Is this intentional? If so you can not just put the data into columns of a matrix because then the columns will be different lengths. Maybe this is why you tried to use a cell array.
In anycase, since you can't use a cell array, and if your groups really have different numbers of elements, than you must enter the data as a vector, and then assign the groups with a group variable that has as many elements as in the data vector.
So you could modify your code as follows and it would work. It would be better to modify the approach further to use some arrays to hold blocks of data, rather than naming them block1, block2 etc, but just to keep a 1:1 comparison with what you did and what would work I haven't changed your original approach too much.
R1rho = rand(1,349);
block1 = R1rho(1:49)
block3 = R1rho(100:149)
block4 = R1rho(150:199)
block5 = R1rho(200:249)
block6 = R1rho(250:299)
block7 = R1rho(300:349)
% assign grouping variables to cell array
driftGrouping = cell(1,299) % 349 - 50 since not using block 2
% assign repeated elements to grouping variable
driftGrouping(1:49) = {'1:49'}; % block1
driftGrouping(50:99) = {'100:149'}; % block3
driftGrouping(100:149) = {'150:199'}; % block4
driftGrouping(150:199) = {'200:249'}; % block5
driftGrouping(200:249) = {'250:299'}; % block6
driftGrouping(250:299) = {'300:349'}; % block7
R1rhoANOVA = [block1, block3, block4, block5, block6, block7]; % vector
anova1(R1rhoANOVA, driftGrouping);

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

### Community Treasure Hunt

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

Start Hunting!