Manipulate data in cells

2 visualizaciones (últimos 30 días)
Rebecca
Rebecca el 11 de Jul. de 2012
I have part of a program that has sorted an nx5 matrix by its fifth column, which contains integers from 1 to 255. The cell "bins" is 255x1, and each of its entries contains the 5-column rows of the original matrix, identified by the entry in the fifth column (ie. the first entry in the cell is 638x5, and for all of its rows, the fifth column contain the number 1). I would like to delete the fifth column from each entry in the bin, and then break each row of four numbers into four separate groups of three (columns 1, 2, 3; columns 1, 2, 4; columns 1, 3, 4; columns 2, 3 4). I am not familiar with the syntax for cell manipulation. Any help with this is appreciated. Thank you!

Respuesta aceptada

Matt Kindig
Matt Kindig el 12 de Jul. de 2012
Hi Rebecca,
I think it would be easiest just to loop through all of the elements of the matrix. To access a given element in the cell array 'bins', use the curly braces {}, in a way that you similarly would use parentheses () for matrices.
For the first part:
output = cell(size(bins));
for k=1:length(bins),
mat = bins{k}; %get element in cell
mat(:,5)=[]; %remove 5th column
output{k} = mat; %assign to output cell array
end
The second part of your request is unclear. Once you break each row into four separate groups of three columns, what do you want to do with the data? Are you assigning each group as an element of the cell array (such that the final cell array is 255x4)? If so, this revised code will work:
output = cell(length(bins), 4);
for k=1:length(bins),
mat = bins{k}; %get element in cell
mat(:,5)=[]; %remove 5th column
output{k,1} = mat(:,[1 2 3]); %break columns into groups and
output{k,2} = mat(:,[1 2 4]); % assign to cell array
output{k,3) = mat(:,[1 3 4]);
output{k,4} = mat(:,[2 3 4]);
end
  1 comentario
Rebecca
Rebecca el 12 de Jul. de 2012
Thank you! The code does exactly what I need!
Background information for this project: Each of the first four rows in the original matrix with five columns, gives the numerical identity of a node, the corner of a tetrahedron. The fifth column gives the numerical identity of the grain that the tetrahedron belongs to - I am working with a material microstructure with 255 grains. The ultimate goal is to find which nodes are shared along boundaries of grains, so when we input this information into our software, we can alter material properties. The logic I am trying to follow (with limited Matlab coding knowledge) will find, in each grain, the shared faces, each face being made up of three nodes. If a face is shared inside a grain, that must mean that the nodes are not along the boundary, and not shared with another grain.
Using the second code you wrote, might you be able to show me how to find and delete the intersections in the "output" matrix for each row? The 4 columns in a row give all the possible faces for that grain. I would like to keep only the ones that are not repeated, and combine them into one list. Then, each list needs to be checked for intersection with all the other 254 lists. The final output needs to be multiple lists that give nodes shared along boundaries. If my logic is correct, there will be 255 choose 2 = 32385 lists, but many will be empty, since not all grains touch each other, or share nodes.
Thank you so much for your help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by