Filter löschen
Filter löschen

store values from loop in an array

1 Ansicht (letzte 30 Tage)
ALDO
ALDO am 20 Mai 2019
Bearbeitet: James Tursa am 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

Akzeptierte Antwort

James Tursa
James Tursa am 20 Mai 2019
Bearbeitet: James Tursa am 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);

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by