how to split values in a cell

2 ビュー (過去 30 日間)
elisa ewin
elisa ewin 2017 年 7 月 28 日
編集済み: Walter Roberson 2017 年 7 月 31 日
Hi!
I have 'out' (attached) and I want to divide the phrases in the first columns based on the
numerical value in the second columns.
I want to obtain three arrays: in the first I want to put the phrases with numerical value
in the second columns <0, in the second I want to put the phrases with numerical value
in the second columns = 0 and in the third I want to put the phrases with numerical value
in the second columns >0
Can you help me?
  2 件のコメント
Shivam Raikundalia
Shivam Raikundalia 2017 年 7 月 28 日
編集済み: Shivam Raikundalia 2017 年 7 月 28 日
Hello,
So if you do not know how large your 3 arrays are going to be and you want them to be exact size I suggest you do something like this:
firstarray={};
secondarray={};
thirdarray={};
for i=1:size(out,1)
if out{i,2}<0
firstarray{end+1}=out(i,1);
elseif out{i,2}==0
secondarray{end+1}=out(i,1);
else
thirdarray{end+1}=out(i,1);
end
end
This method is a little slow however since one of the arrays change size on every iteration. Hope this helps!
elisa ewin
elisa ewin 2017 年 7 月 31 日
thanks, it runs and it's helpful

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

回答 (1 件)

Star Strider
Star Strider 2017 年 7 月 28 日
If you have R2015b or later, use splitapply:
D = load('matlab.mat');
out = D.out;
[G, ID] = findgroups([out{:,2}]);
Result = splitapply(@(x){x}, out(:,1), G');
  2 件のコメント
elisa ewin
elisa ewin 2017 年 7 月 31 日
The code gives an error:
Undefined function 'findgroups' for input arguments of type 'double'.
Walter Roberson
Walter Roberson 2017 年 7 月 31 日
編集済み: Walter Roberson 2017 年 7 月 31 日
As Star Strider indicated, findgroups requires R2015b or later.

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by