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 visualizzazione (ultimi 30 giorni)
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" ];

Risposte (1)

KSSV
KSSV il 20 Gen 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 Commento
Sarath J
Sarath J il 20 Gen 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" ];

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by