operator '==' not supported for operands of type 'cell'

323 ビュー (過去 30 日間)
Kayla Black
Kayla Black 2020 年 6 月 4 日
コメント済み: Walter Roberson 2025 年 2 月 18 日 18:13
While there are some useful answers to this type of question, a lot of them are too advanced for me to understand/use as a beginning MATLAB student. I am trying to loop through rows of a table to find instances in which in a particular index is equal to something. Here is a picture of what my table looks like:
and I am interested in comparing when a subject has responded true or false to a certain verb ('smashed' is one of 3). My idea for doing this would be to make a for loop such as the following:
smashedFalseCounter = 0;
for i = 1:height(myTable)
if myTable.verb(i) == 'smashed'
if expTable2.response(i) == false
smashedFalseCounter = smashedFalseCounter + 1;
end
end
end
And I get the following error message:
  1 件のコメント
Mohammad Sami
Mohammad Sami 2020 年 6 月 4 日
Use string comparison functions.
strcmp(myTable.verb{i}, 'smashed')

サインインしてコメントする。

回答 (3 件)

madhan ravi
madhan ravi 2020 年 6 月 4 日
ismember(myTable.verb(i), 'smashed')
  1 件のコメント
madhan ravi
madhan ravi 2020 年 6 月 4 日
編集済み: madhan ravi 2020 年 10 月 12 日
And by the way you don’t need a loop.
Counts = nnz(ismember(myTable.verb(myTable.Response == true), 'smashed'))

サインインしてコメントする。


Gizachew Dessalegn
Gizachew Dessalegn 2025 年 2 月 18 日 13:56
編集済み: Walter Roberson 2025 年 2 月 18 日 18:09
for s = 1:ncntry
for r = 1:ncntry
Esr14{s,r} = zeros(nsec, 1);
if r ~= s
for t = 1:ncntry
if t ~= s && t ~= r
% Access cell contents using {} and perform operations
term = (va_cs(1,t) * Bmat(t,s))'; % Now numeric values
Esr14{s,r} = Esr14{s,r} + term .* EXGR_FNL_cs_c(s,r);
end
end
end
end
end
Operator '*' is not supported for operands of type 'cell'.
  1 件のコメント
Walter Roberson
Walter Roberson 2025 年 2 月 18 日 18:13
Based on the above code, we can guess that either va_cs or Bmat are cell arrays. The code might have to be
term = (va_cs{1,t} * Bmat(t,s))';
or
term = (va_cs(1,t) * Bmat{t,s})';
or
term = (va_cs{1,t} * Bmat{t,s})';
There is also a significant chance that EXGR_FNL_cs_c is a cell array, so the line after that might need to be
Esr14{s,r} = Esr14{s,r} + term .* EXGR_FNL_cs_c{s,r};

サインインしてコメントする。


Steven Lord
Steven Lord 2025 年 2 月 18 日 15:54
Another possibility for the original question would be to create a pivot table or to use groupcounts or other grouping functions. Let's make a sample table:
subject = (1:9).';
verbs = {'smashed', 'crushed'};
n = numel(verbs);
verb = verbs(randi(n, 1, 9)).';
response = rand(9, 1) > 0.5;
t = table(subject, verb, response)
t = 9x3 table
subject verb response _______ ___________ ________ 1 {'crushed'} false 2 {'smashed'} false 3 {'smashed'} false 4 {'crushed'} true 5 {'crushed'} true 6 {'smashed'} true 7 {'crushed'} false 8 {'smashed'} true 9 {'crushed'} true
Now let's create a pivot table with response as the variables and the verbs as the rows.
P = pivot(t, Columns='response', Rows='verb')
P = 2x3 table
verb false true ___________ _____ ____ {'crushed'} 2 3 {'smashed'} 2 2
Or take the counts of each group defined by unique combinations of verb and response.
G = groupcounts(t, {'verb', 'response'})
G = 4x4 table
verb response GroupCount Percent ___________ ________ __________ _______ {'crushed'} false 2 22.222 {'crushed'} true 3 33.333 {'smashed'} false 2 22.222 {'smashed'} true 2 22.222

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by