store values from loop in an array

1 vue (au cours des 30 derniers jours)
ALDO
ALDO le 20 Mai 2019
Modifié(e) : James Tursa le 20 Mai 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

Réponse acceptée

James Tursa
James Tursa le 20 Mai 2019
Modifié(e) : James Tursa le 20 Mai 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);

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by