Matrix row cell concatenation

Hello,
I have a matrix and I would like to combine its row cells as an array of the matrix.
Ex:a
1 2 3
4 5 6
7 8 9
result:b
123
456
789
I tried a solution to from this link:
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
but I get the out put as
ans =
[1x3 double]
[1x3 double]
[1x3 double]
any help would be appreciated. Thnx

4 件のコメント

Image Analyst
Image Analyst 2019 年 7 月 21 日
Please learn the terminilogy by reading the FAQ: What is a cell array.
If you have a matrix of numbers, MATLAB calls those elements while Excel will call them cells. "Cells" are a very different thing in MATLAB.
You said you had a matrix, so in MATLAB you'd use bracket, and preferably use commas between columns, and semicolons between rows, though those can be optional in MATLAB. however it's better if you use them:
a = [...
1, 2, 3; ...
4, 5, 6; ...
7, 8, 9];
Now you refer to "its row cells" but it's not clear what you mean by that. Do you mean cells, like we use them in Excel (which are essentially just numbers) or like we use them in MATLAB where cells are a different type of variable entirely?
Now you said you want an output, b:
123
456
789
but that is not clear.
  1. Is that [123; 456; 789], which is a 3-by-1 column vector of 3-digit numbers?
  2. Or do you mean {'123'; '456'; '789'} which is a 3-by-1 cell array of strings?
  3. Or do you want a 3-by-1 array of cells where each cell contains a 1-by-3 row vector of numbers, like {[1,2,3]; [4,5,6]; [7,8,9]}?
These are all different concepts and we can't give you an answer until you say which you want. Since you probably don't know, then why don't you just tell us what you plan on doing with your output, when you get it, and we'll tell you what kind of output you should create?
Ahsan
Ahsan 2019 年 7 月 21 日
Thank you for correcting and sorry for the misleading question. I will keep this in mind next time. Much appreciated.
Ahsan
Ahsan 2019 年 7 月 21 日
and I am trying to achieve 1.[123; 456; 789], a 3-by-1 column vector of 3-digit numbers?
Stephen23
Stephen23 2023 年 4 月 11 日
M = [1,2,3;4,5,6;7,8,9];
V = M*[100;10;1]
V = 3×1
123 456 789

サインインしてコメントする。

回答 (1 件)

madhan ravi
madhan ravi 2019 年 7 月 20 日
編集済み: madhan ravi 2019 年 7 月 20 日

1 投票

b = reshape(arrayfun(@(x)polyval(a(x,:),10),...
1:size(a,1),'un',0),[],1);
Wanted = cell2mat(b)

4 件のコメント

madhan ravi
madhan ravi 2019 年 7 月 20 日
Note: Works only for single digit number.
Ahsan
Ahsan 2019 年 7 月 20 日
how do I make it so it works for two digits?
madhan ravi
madhan ravi 2019 年 7 月 20 日
編集済み: madhan ravi 2019 年 7 月 22 日
What more do you need ? Doesn't the below give what you want??
>> a = [1 2 3
4 5 6
7 8 9 ];
>> bb=num2cell(string(a),1);
>> b=str2double(strcat(bb{:}))
b =
123
456
789
>> size(b)
ans =
3 1 % 3 by 1 vector
>>
Andrew Sol
Andrew Sol 2023 年 4 月 11 日
Here! That's what I need! Thank you!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

質問済み:

2019 年 7 月 20 日

コメント済み:

2023 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by