0

I am new to MATLAB. I have a data structure named da. I want to sort the first column of da.mat and want to let da.rid and the other columns to follow the rearranged order. da.cid contains the column names and da.rid contains the row IDs.

da = 
    mat: [22268x377 single]
    rid: {22268x1 cell}
    rhd: {''}
  rdesc: {22268x1 cell}
    cid: {377x1 cell}
    chd: {0x1 cell}
  cdesc: {377x0 cell}

Also, if I want to use some other column instead of the first column of da.mat and which I will get from da.cid, how can I acheive it? For example, if I want to look for the column name 'A02' in cid and use it to select the specific column of da.mat for sorting. Could you please help me? Thanks.

Woody

Divakar
  • 218,885
  • 19
  • 262
  • 358
woodylin
  • 17
  • 4
  • Thanks for the comment. My question is actually very different. I want to sort one of the columns in the data structure and make the other columns (especially the rid) follow the same order. – woodylin May 15 '14 at 03:36
  • I withdraw my duplicate – JamesENL May 15 '14 at 03:44
  • By `other columns` what do you mean? That is `other` columns of other fields? If so, how is that possible, because other fields aren't of the same size as `da.mat`. – Divakar May 15 '14 at 03:51
  • Yes, I meant the other columns in the da.mat itself. – woodylin May 15 '14 at 04:21

1 Answers1

0

Assuming by other columns, you mean other columns of da.mat itself, you may try this -

[val,ind] = sort(da.mat(:,1))
da.mat = da.mat(ind,:)
da.rid = da.rid(ind)

If you are looking to use some other column number instead of 1 for sorting and based on the names in the field cid, use this -

cid_matchcol =  'A02'; %// column name to be used from `da.cid` to choose column of `da.mat`

base_col = find(strcmp(da.cid,cid_matchcol),1)
[val,ind] = sort(da.mat(:,base_col))
da.mat = da.mat(ind,:)
da.rid = da.rid(ind)
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • By sorting the first column with da.mat(:,1), you extracted its value and index. Then, you assign the index to rid. Yes, this is what I wanted! Thanks!! – woodylin May 15 '14 at 04:18
  • Hi Divakar, Could you explain more closely what da.mat(ind,:) does? Aren't ind only the index? How does the matlab know which column to sort? – woodylin May 15 '14 at 05:17
  • `da.mat(ind,:)` has `:` that tells MATLAB to sort all columns. If I had written `da.mat(ind,2)`, it will sort only the second column, `da.mat(ind,3)` for the 3rd column and so on. – Divakar May 15 '14 at 05:31
  • Also, Can I specify the column names with cid for sort(da.mat(:,1))? – woodylin May 15 '14 at 05:32
  • column names or column numbers? – Divakar May 15 '14 at 05:33
  • cid contains the column names. – woodylin May 15 '14 at 05:35
  • Well you can't index into columns of matrices with just names and by names I am assuming strings. You need numbers starting from 1. Like if column name is `'col1'`, you can't say `da.mat(ind,'col1')`, as it doesn't make sense to MATLAB. – Divakar May 15 '14 at 05:36
  • But I should be able to derive the column index from column names, right? For example, `col1` has a index number `1` in `cid`. – woodylin May 15 '14 at 05:38
  • Give us some sample `cid` values? Those might give us some idea on what you are looking for though. `'col1'` was just an example. What if in `cid` you have names like {'john', 'mike'}, how would MATLAB know then? – Divakar May 15 '14 at 05:39
  • `cid` has column names such as `A01`, `A02`, `A03` and so forth. I hope to sort the `A02` column. `A02` should be the second column. It should be written as `sort(da.mat(:,2))`. I just want to replace the `2` with `A02`. – woodylin May 15 '14 at 05:41
  • I am thinking if we could derived the index number from `cid` based on the column name `A02`. And, we could put the index number to replace `2`. – woodylin May 15 '14 at 05:45
  • Thanks! It is what I was expecting! The `find()` function! – woodylin May 15 '14 at 05:58
  • Maybe add this extra requirement of using `da.cid` to index into the columns of `da.mat` into your question and thus make it very safe from attributing it as a duplicate question? This way the additional code would stay in context too, as comments gets deleted by mods often. – Divakar May 15 '14 at 06:01
  • I have edited my question. Please take a look if I need to add more things. Thanks for helps sincerely. – woodylin May 15 '14 at 06:06
  • Made few edits to your question. Hope they didn't diverge from your original intentions. – Divakar May 15 '14 at 06:15
  • Follow this [answer](http://stackoverflow.com/a/23511916/3293881) and use the final 5 lines of the code – Divakar May 15 '14 at 06:30