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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by