store values from loop in an array

1 次查看(过去 30 天)
I want to compare each individual element of A with each element of B and store the logical answer in a table. Thanks for the help!
A= categorical ({'A','B','C'});
B= categirial({'B','D'});
TableA = zeros(3,2);
for i = length(A)
for j=1:length(B)
if isequal (A(i),B(j))
Answer=1;
tableA (i,j) = [Answer];
else
Answer=0;
tableA (i,j)= [Answer];
end
end
end

采纳的回答

James Tursa
James Tursa 2019-5-20
编辑:James Tursa 2019-5-20
Typos in your code:
for i=1:length(A)
And change tableA to TableA (MATLAB is case sensitive).
Or, you could get rid of the loops entirely:
TableA = (A'==B);
On older versions of MATLAB:
TableA = bsxfun(@eq,A',B);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by