簡體   English   中英

如何在MATLAB中將單元格數組值分成兩列?

[英]How to split cell array values into two columns in MATLAB?

我在單元格數組中有數據,如此處的變量查看器所示

{[2.13949546690144;56.9515770543056],
 [1.98550875192835;50.4110852121618],
 ...}

我想將其分為兩列,兩個小數點為:

2.13   56.95 
1.98   50.41

通過刪除大括號和分號(例如[;] (類似於Excel中的“文本到列”)。

如果您的N元素單元格數組C在每個單元格中都有2×1數值數據,您可以像這樣輕松地將其轉換為N×2數值矩陣M (使用round函數將每個元素四舍五入為2個有效數字):

M = round([C{:}].', 2);

語法C{:}創建一個逗號分隔C內容列表 ,等效於C{1}, C{2}, ... C{N} 這些都使用[ ... ] 水平連接 ,然后使用進行轉置結果.'

% let's build a matching example...
c = cell(2,1);
c{1} = [2.13949546690144; 56.9515770543056];
c{2} = [1.98550875192835; 50.4110852121618];

% convert your cell array to a double array...
m = cell2mat(c);

% take the odd rows and place them to the left
% take the even rows and place them to the right
m = [m(1:2:end,:) m(2:2:end,:)];

% round the whole matrix to two decimal digits
m = round(m,2);

根據您的環境設置,您可能仍會在前兩位十進制數字后看到很多結尾的零...但不要擔心,一切都很好(從精度角度來看)。 如果只想顯示數字的“實數”數字,請使用以下命令:

format short g;

你應該使用cell2mat

A={2.14,1.99;56.95,50.41};
B=cell2mat(A);

關於四舍五入,您可以執行以下操作:

B=round(100*B)/100;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM