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 回表示 (過去 30 日間)
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" ];

回答 (1 件)

KSSV
KSSV 2023 年 1 月 20 日
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 件のコメント
Sarath J
Sarath J 2023 年 1 月 20 日
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" ];

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by