How can I dynamically create a cell array of cell arrays?

I am iterating through a cell to match the id row from one cell array 1 to the id row from another cell array 2. I am trying to dynamically retrieve all of the values from cell array 1 when the value from cell array 2 matches.
Not sure how to create dynamic cell structures as shown below with my 'housesPerCluster' variable. Inside my if statement, I want it to add the value to a cell when it finds a matching iterClusterId.
I am stuck on the syntax for how to create a new cell 'housesPerCluster' with cells inside of it that align with the ClusterId. So I want all of the house Ids that match clusterId 1, 2, and so on, up to the number of clusters (numClusters). My code to loop looks like this:
housesPerCluster = cell{1,numClusters};
for iterClusterId = 1:numClusters
for iterRow = 1:houseNum
if idAndCluster{1,2}{iterRow,1} == iterClusterId
housesPerCluster{iterClusterId}{end+1} = idAndCluster{1,1}{iterRow,1};
end
end
end
Thanks

2 Comments

I figured out the syntax, I had to change the housesPerCluster variable to work like so:
housesPerCluster = {};
for iterClusterId = 1:numClusters
for iterRow = 1:houseNum
if idAndCluster{1,2}{iterRow,1} == iterClusterId
housesPerCluster{iterClusterId,end+1} = idAndCluster{1,1}{iterRow,1};
end
end
end
And I got the output I wanted. Thanks.
Glad that you found out but do read the link below

Sign in to comment.

Categories

Asked:

on 23 Oct 2018

Commented:

on 23 Oct 2018

Community Treasure Hunt

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

Start Hunting!