I have a two column item 1st, Column is child's name and 2nd column is his/her choice of toys. For a given list of Toys I bought, make a table against each toy to each child.

1 visualización (últimos 30 días)
A=["Child1" "Dog"
"Child1" "Cat"
"Child1" "Babbie"
"Child2" "Tractor"
"Child2" "Plane"
"Child2" "Bus"
"Child2" "Car"
"Child3" "Helicoptor"
"Child3" "Crane"
"Child3" "Tiger" ];
n=length(A);
Unrecognized function or variable 'A'.
m=length(M);
%AA= [Toy Name; Which Child does the Toy belong?]
AA=zeros(n,1);
%
for i=1:n
for j=1:m
if A(i,1)== M(j,2)
AA(i,1)= M(j,1);
end
end
end
AA;
%A= Child Toys
A=["Child1" "Dog"
"Child1" "Cat"
"Child1" "Babbie"
"Child2" "Tractor"
"Child2" "Plane"
"Child2" "Bus"
"Child2" "Car"
"Child3" "Helicoptor"
"Child3" "Crane"
"Child3" "Tiger" ];
%AA=[Toys Child ]<---Need the answer as below; I'm new to String arrays use of for/if loops
AA=[ "Babbie" "Child1"
"Bus" "Child2"
"Dog" "Child1"
"Helicoptor" "Child3"
"Tiger" "Child3" ];

Respuestas (1)

KSSV
KSSV el 20 de En. de 2023
A=["Child1" "Dog"
"Child1" "Cat"
"Child1" "Babbie"
"Child2" "Tractor"
"Child2" "Plane"
"Child2" "Bus"
"Child2" "Car"
"Child3" "Helicoptor"
"Child3" "Crane"
"Child3" "Tiger" ];
child = A(:,1) ;
toy = A(:,2) ;
T = table(child,toy)
T = 10×2 table
child toy ________ ____________ "Child1" "Dog" "Child1" "Cat" "Child1" "Babbie" "Child2" "Tractor" "Child2" "Plane" "Child2" "Bus" "Child2" "Car" "Child3" "Helicoptor" "Child3" "Crane" "Child3" "Tiger"
  1 comentario
Sarath J
Sarath J el 20 de En. de 2023
My answer should be the below, because only a few of the total list of toys were bought.
AA=[ "Babbie" "Child1"
"Bus" "Child2"
"Dog" "Child1"
"Helicoptor" "Child3"
"Tiger" "Child3" ];

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by