store values from loop in an array

1 view (last 30 days)
ALDO
ALDO on 20 May 2019
Edited: James Tursa on 20 May 2019
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

Accepted Answer

James Tursa
James Tursa on 20 May 2019
Edited: James Tursa on 20 May 2019
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);

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!