필터 지우기
필터 지우기

A for loop extracting every new variable into column vectors?

조회 수: 2 (최근 30일)
Ana Castanheiro
Ana Castanheiro 2018년 2월 6일
댓글: Ana Castanheiro 2018년 2월 7일
Hi! I have the following stacked table, and I need to be able to extract/refer to every new element that appears at ChElem_Indicator (column 8) with the correspondent value that appears at ChElem (column 9). The ChElem_Indicator contains repeated data, with the elements (for example "Si") appearing hundreds of times.
Every time a new ChElem_Indicator variable appears, I'd like to create a column vector (with the name of that variable) that extracts the corresponding value from ChElem column. Scanning the entire table, my goal is to end up with, for example, a column vector "Si" with all values which are related to "Si", and to have this for all the different variables that appear in ChElem_Indicator column (not only "Si"). I think a for loop is what I need, but I don't know how to incorporate the part "for every new variable".
Any ideas/suggestions? Thanks in advance!
  댓글 수: 2
Stephen23
Stephen23 2018년 2월 6일
편집: Stephen23 2018년 2월 6일
'I think a for loop is what I need, but I don't know how to incorporate the part "for every new variable".'
Do NOT do this! Magically accessing variable names in a loop is how beginners force themselves into writing slow, complex, buggy code:
'my goal is to end up with, for example, a column vector "Si" with all values which are related to "Si", and to have this for all the different variables that appear in ChElem_Indicator column (not only "Si").'
'Any ideas/suggestions?'
Use one of the table class methods to group the rows based on that column, such as
or
Ana Castanheiro
Ana Castanheiro 2018년 2월 7일
Thanks for sharing your advices and suggestions, these are especially important for a Matlab beginner as myself.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2018년 2월 6일
[groupid, groupname] = findgroups(ClustSEMdata.ChElem_Indicator);
elements = splitapply(@(v) {v}, ClustSEMdata.ChElem, groupid);
result = cell2table(elements.', 'VariableNames', groupname)
It sounds like you want to create individual variables for each element, which would be a very bad idea. Putting them in a table as above, a cell array, a map, or a structure would be a lot better.
  댓글 수: 1
Ana Castanheiro
Ana Castanheiro 2018년 2월 7일
Many thanks for your reply Guillaume. This piece of code works nicely but only partially for what I was asking about. Based on your and Stephen's comments, I now understand that creating individual variables for each element is not a good idea. I had created the table in the image using the stack function, but probably this is not necessary at all, and I should instead work with the original table (where every column is already a different chemical element).
What I need is to be able to do some clustering analysis, for which I need to start with the code below. So, perhaps the best is that I use:
treedata = [ originaldata.Al, originaldata.Si, originaldata.Fe];
%%desired treedata = [Al, Si, Fe];
dist = pdist(treedata,'euclidean');
linkmethod = 'ward';
tree = linkage(dist,linkmethod);
figure()
dendrogram(tree, 0, 'orientation', 'right')
title(sprintf('%s%s %d', det1{1:2},det1{3}))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by