-2

I would like to covert three <1xN cell> (A, B and C) into a single Nx3 matrix. Could someone help me with this?

C={{1xN}; {1xN}; {1xN}};

where each N is a number in single quotes, e.g.

C = {{'123123' ,'12324', ....N times}; {'123123', '12324', ....N times}; {'123123', '12324' ,....N times}}

Since a couple of them mentioned about the ridiculous input, this is the reason for having it in the above form.

The three nested array of cells are the results of a regexp where my string and expression are both strings. Therefore I have the output of regexp as three cell arrays of row vectors. For e.g.

node_ids=regexp(nodes,'(?<=node id=")\d*','match');

I can use cat function and then use a str2double for all three cell arrays and finally form a matrix by cell2mat. For e.g.

node_ids=cat(1,node_ids{:});node_ids=str2double(node_ids);

But this takes more time and has more LOC. My question is can it be done with fewer lines of code?

I tried using the cat function but keep getting this error:

Cannot support cell arrays containing cell arrays or objects.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
EyesOfÖzil
  • 309
  • 1
  • 9
  • 2
    Mind telling us what is *in* the cells? – Adriaan Oct 14 '15 at 17:43
  • C={{1xN}; {1xN}; {1xN}}; where each N is a number in single quotes(for ex. C = {{'123123' '12324' ....N times}; {'123123' '12324' ....N times}; {'123123' '12324' ....N times}} hope this gives more clarity about the question – EyesOfÖzil Oct 14 '15 at 18:43

1 Answers1

1

Your input data is pretty bad.... why are you using a nested array of cells where each element is a string?

In any case, assuming C is your original input data, do this:

C = {{'123123' '12324'}; {'123123' '12324'}; {'123123' '12324'}};
out = cellfun(@(x) cellfun(@str2num, x, 'uni', 0), C, 'uni', 0);
out = cell2mat(cellfun(@cell2mat, out, 'uni', 0));

First line is some dummy data. Next line first goes through every nested cell element over your cell array and converts the strings into numbers. However, these are still in cell arrays. As such, the next line converts each cell array in the nested cell into a matrix, then we merge all of the cells together into one final matrix.

We get:

>> out

out =

      123123       12324
      123123       12324
      123123       12324
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 2
    "Pretty bad" is being nice! – IKavanagh Oct 14 '15 at 19:53
  • Hey guys @IKavanagh , I have edited my question for using this type of input data. Also, I used the above method and i got the following error: Error using str2num (line 33) Requires string or character array input. I used str2double instead and got no errors but instead got NaN values in my matrix. Any reason for this? – EyesOfÖzil Oct 15 '15 at 12:36
  • 1
    @EyesOfÖzil If you get that error with the above method then your input is malformed and not as described in the question or demoed in this answer. This answer works perfectly for me. Maybe you could post the specific data that is causing you an issue? – IKavanagh Oct 15 '15 at 14:01
  • The input is this C = {{node_ids}; {node_a}; {node_b}}; where node_ids, node_a and node_b are the matches using regular expression. They are 1xN cells where each cell element is a string. Hope i am clear enough? – EyesOfÖzil Oct 15 '15 at 14:16
  • I guess the reason for that error is C = {{node_ids}; {node_a}; {node_b}}; is not the same as C = {{'123123' '12324'}; {'123123' '12324'}; {'123123' '12324'}}; sorry for the confusion created. @IKavanagh – EyesOfÖzil Oct 15 '15 at 15:07
  • Can you do `C = {node_ids; node_a; node_b};`? – IKavanagh Oct 15 '15 at 15:11
  • thanks for the help guys! It worked fine with this C = {node_ids{:}; node_a{:}; node_b{:}}; – EyesOfÖzil Oct 22 '15 at 14:15