How do I concatenate cells in each row in a matrix?

Dear Matlab users,
I need to cancat each row's cell in a matrix, For example: I have this matrix:
8 1 6
3 5 7
4 9 2
My goal is to have this form: 816, 357, 492.
I am beginner with matlab. I would be very grateful if you could help me. Thanks in advance.

1 Comment

"My goal is to have this form: 816, 357, 492."
Can you please explain what you want: is this three numbers with values 816, 357, and 492, or three separate vector of integers {[8,1,6],[3,5,7],[4,9,2]}, or is this three strings {'816','357','492'}, or something else entirely?
Your description is not very clear what you want the output to be.

Sign in to comment.

 Accepted Answer

a = [ 8 1 6 ; 1 2 3 ; 4 5 6 ]
[l c ] = size (a) ;
b = cell (l,1);
for i =1 : l
b {i,: } = [ a(i,1) a(i,2) a(i,3) ] ;
end

7 Comments

I doubt it's what the OP was asking, but in any case, the above can be achieved a lot more simply (and reliably since it makes no assumption on the number of columns) with just one line:
b = num2cell(a, 2);
Furthermore, the expression
b{i,: } = [ a(i,1) a(i,2) a(i,3) ]
is the same as
b{i} = a(i, [1:3]); %however b{i} = a(i, :) would make more sense
Note that since you declared b with just one column, the : in b{i, :} is just 1 and can just as well be omitted.
Dear users, I tried you last solutions:
a = importdata('file.mat') % file.mat contains my matrix
[l c ] = size (a)
b = cell (l,1)
for i =1 : l
b{i} = a(i, [1:16]); %however b{i} = a(i, :)
end
I got this solution:
b =
{
[1,1] = [](0x0)
[2,1] = [](0x0)
[3,1] = [](0x0)
[4,1] = [](0x0)
[5,1] = [](0x0)
[6,1] = [](0x0)
[7,1] = [](0x0)
[8,1] = [](0x0)
[9,1] = [](0x0)
[10,1] = [](0x0)
[11,1] = [](0x0)
[12,1] = [](0x0)
[13,1] = [](0x0)
[14,1] = [](0x0)
[15,1] = [](0x0)
[16,1] = [](0x0)
[17,1] = [](0x0)
.....
But my problem is that i don't understand the result, what does it mean [](0x0)?
Thank you very much for your help.
Why did you accept the answer if it does not work for you?
I'm not sure what you've done. The output you're showing does not appear to be something that matlab could produce. In any case, as per my comment, the theoretical output of the above code can be achieved more simply with:
a = importdata('file.mat');
b = num2cell(a, 2);
Also as per my comment, I doubt that's what you're asking for.
I am so sorry, it is the first time that i use this forum.
@Guillaume: Octave produces that output.
@Smache Meriem: You should only accept an answer when it resolves your question.
@Stephen: yes sir, exactly i use octave, for that i am not sur about the solution that i have.
@Guillaume and @ AMAL targhi thank you very much for your help.

Sign in to comment.

More Answers (1)

num2cell(a,2)

1 Comment

Thank you very much for your help, however sir the size of my matrix id 100*16:
208 15 217 252 128 35 50 252 209 120 97 140 235 220 32 251
231 174 143 43 125 66 49 143 48 139 81 103 154 229 93 229
32 10 237 65 224 22 83 238 31 15 252 27 179 48 173 221
233 18 178 101 90 109 225 184 210 168 183 185 190 169 96 205
161 133 149 18 115 65 120 123 163 227 105 157 98 240 221 142
24 24 208 175 246 76 103 163 4 28.........
So by trying your method i have this result: operator *: nonconformant arguments (op1 is 100x16, op2 is 3x1) I would be very grateful sir if you could help me. Thanks in advance.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Tags

Asked:

MS
on 15 Jul 2016

Commented:

MS
on 16 Jul 2016

Community Treasure Hunt

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

Start Hunting!