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 view (last 30 days)
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" ];

Answers (1)

KSSV
KSSV on 20 Jan 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 Comment
Sarath J
Sarath J on 20 Jan 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" ];

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!