I want to sort a field structure that is a vector field. You can see in the image.
I tried the following method but it doesn't works.
D=struct2cell(A) tam=size(D) D = reshape(D, tam(1), []) D=D' D=sortrows(D,1)
when I do the last command matlab error message apears:
Error using sortrows>sort_cell_back_to_front (line 130) Some cells in X contain non-scalar values and cannot be sorted.
Error in sortrows (line 88) ndx = sort_cell_back_to_front(x_sub, col);
I think it's because the first field is a vector field of type double.
How can I solve this?
Thanks in advance.

 채택된 답변

Guillaume
Guillaume 2017년 5월 26일

1 개 추천

Assuming that you want to sort by antecedent
[~, order] = sortrows(vertcat(A.antecedent));
A = A(order)

댓글 수: 5

alfred
alfred 2017년 5월 29일
편집: alfred 2017년 5월 29일
This function works perfectly! Thank you!
Could you tell me please where i can find this on Mathworks?
If not appears on Mathworks.. maybe in other site.
elseif you can explain me this function?
And sorry, if I have rows with none value ([]), this function deletes these positons. I want that doesn't happens.
What can I do?
I can solve this part creating an other variable with all positions and compare 1 by 1. Then it's not really important how to do this. The follow question yes but.
I want that the column consequent change with the same rows of antecedent, because any row of antecedent have his consequent value.
Can I do this with the same function that you show me? or i need to do an other function?
Really thanks!
Jan
Jan 2017년 5월 29일
편집: Jan 2017년 5월 29일
Do you mean:
doc sortrows
? This can be found on the MathWorks pages also: https://www.mathworks.com/help/matlab/ref/sortrows.html. Or do you want to know, how commands like sortrows can be found? Then: You are looking for something concerning sorting. Start with:
doc sort
Because this does not solve you problem only almost, search the "See also" line at the bottom. Or search the term in general:
docsearch sort
alfred
alfred 2017년 5월 29일
편집: alfred 2017년 5월 29일
Thanks men!
I was looking this functions days ago (sort, sortrows) and now I look vertcat. But I don't find any part for sort the vectors in an array.
the dubt is but, I don't know if I understand the first part [~ order].
This means that, all the vectors that are diferent, are be ordered? I supose it ordered by ascending (default).
It is?
And realy sorry for the other question... I have not seen that the consequent change too with the same order. :P
Thanks for all.
@alfred, I don't understand what you mean by "I want that the column consequent change with the same rows of antecedent, because any row of antecedent have his consequent value". Can you show an example of input and desired output?
[~, order] = ...
the ~ means: I don't care about this output. You only want the second output of sortrows, the order of the indices.
alfred
alfred 2017년 5월 29일
@Guillaume, Sorry how i said before i have seen that the consequent change with antecedent 1 by 1. then i answerd me...
And i don't understand totally your answer.. because... this function ordered with first output too... or is what it seems... i tried to put all disordered and it works correctly. always put [1 1] in the first row then what you mean with first output?
I'm really confused..

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Install Products에 대해 자세히 알아보기

질문:

2017년 5월 26일

댓글:

2017년 5월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by