フィルターのクリア

How can i combine two column of a matrix into one column?

2 ビュー (過去 30 日間)
Md. Touhidul
Md. Touhidul 2012 年 6 月 29 日
I have a matrix of order m by 3. All three (3) columns contain numeric values; I want to do a sequence of operation as follows:
  1. Convert the last two column into text while the first one remain numeric;
  2. Combine the last two columns that have now text values into a one column hence the matrix order will be reduce to m by 2;
Please guide me how can i do that. I am a new user of matlab. I would be very thankful for urgent reply.
T. Islam
  1 件のコメント
Jan
Jan 2012 年 6 月 29 日
編集済み: Jan 2012 年 6 月 29 日
Please post a minimal example. What is the expected result for: [1,2,3; 4,5,6]? Consider, that you cannot mix numerical and string data in one matrix, but this is possible in a cell.
The term "urgent" is counter-productive in this forum, which is supported by volunteers in their spare-time. I'd suggest to delete it.

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

回答 (1 件)

Adam Filion
Adam Filion 2012 年 6 月 29 日
編集済み: Adam Filion 2012 年 6 月 29 日
You cannot mix data types inside of a matrix, but you could do this instead inside of a cell. For example
A = magic(3);
B = cell(size(A,1),size(A,2)-1);
for i=1:3
B{i,1} = A(i,1);
B{i,2} = [num2str(A(i,2)) ' ' num2str(A(i,3))];
end
[EDIT] changed code to better match problem description
  2 件のコメント
Ryan
Ryan 2012 年 6 月 29 日
編集済み: Ryan 2012 年 6 月 29 日
To combine the last two columns into a string:
A = magic(3);
A = num2cell(A);
for i=1:size(A,1)
A{i,2} = [num2str(A{i,2}),num2str(A{i,3})];
end
A(:,3) = [];
A
Input A:
8 1 6
3 5 7
4 9 2
Output A:
8 '16'
3 '57'
4 '92'
Adam Filion
Adam Filion 2012 年 6 月 29 日
Forgot about removing the last column with [], good way to go about it to avoid creating a new variable.

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by