How to separate rows from an array

Hey guys,
I am fairly new to MatLab and I am trying to figure out how to seperate out certain rows from my main array into a new array based on the contents of my first column.
For Example
column 1 column 2 column 3
1 Cash 5.00
2 Credit 10.00
3 Debt 1.50
So lets say I wanted to separate out cash and credit and add them to a new array so I can ultimately sum the contents of column 3 together.
Something along the lines of "if column 1 is => 0 & =<2, move whole rows to " array 2"
Thanks for your help.

1 件のコメント

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日
You can use {}Code format next time.

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

回答 (3 件)

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日

0 投票

If you matrix is numerical, it is actually easy.
a=[1 5.00
2 10.00
3 1.50
1 5.00
2 10.00
3 1.50];
index=a(:,1)>0 & a(:,1)<3;
b=a(index,:)

12 件のコメント

Alex
Alex 2011 年 10 月 27 日
Wow this is awesome, Thank you. and thank you Honglei for your input too.
Alex
Alex 2011 年 10 月 27 日
OK so I am having another problem.
I am sorting the 1st column correctly but the data from the rest of the respective rows are not copying over with it.
i.e. after using
index=a(:,1)>0 & a(:,1)<3;
b=a(index,:)
it only gives me
1
2
but I need
1 cash 5.00
2 credit 10.00
How do I do this?
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日
What is your a? Can you give it exactly using MATALB syntax?
Alex
Alex 2011 年 10 月 27 日
Ok so I have a structure array with 4 arrays
lets say struct.1, struct.2,struct.3,struct.4
.1 has a list of numbers as double
.2 has a list of 'text'
.3 has a list of number values as cells
.4 has a list of number values as cells
So far I have
index=struct.1(:,1)>0 & struct.1(:,1)<3;
b=struct.1(index,:)
which is working but I need the corresponding values in the other 3 structs to move with the first struct.
I hope this is not too cumbersome
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日
The problem is, you can't have '1' or '2' as the field name for a structure. That's why I ask you to provide the exact a data using valid MATLAB syntax. Any way, here is how it's done if a is a struct array.
%%
a=struct('f1',{1,2,3},'f2',{'Cash','Credit','Debt'},'f3',{5,10,1.5})
Num=[a.f1];
index=Num>0 & Num<3;
b=a(index);
b(1)
b(2)
ans =
f1: 1
f2: 'Cash'
f3: 5
ans =
f1: 2
f2: 'Credit'
f3: 10
Alex
Alex 2011 年 10 月 27 日
ok sorry it's
struct.GL
struct.descrp
struct.preQE
struct.curQE
but my data set is over 250 rows long. is there any short hand for the first step where I have to manually enter the data in the { }?
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日
Then where did you get the data? How do you know if it is struct.GL etc. if you don't even have it in MATLAB yet?
Alex
Alex 2011 年 10 月 27 日
I have a loaded spread sheet named bs_data (250x4) that I am drawing the data from
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日
use [NumData,TxtData,a]=xlsread('bs_data.xls'), you'll get a as a cell array and then use the following.
%%
a={1,'Cash',5.00
2,'Credit',10.00
3,'Debt',1.50}
Num=[a{:,1}];
index=Num>0 & Num<3;
b=a(index,:)
Alex
Alex 2011 年 10 月 27 日
Ok, nice thanks. But in actuality what would I enter into a={} I have about 30 different line items.
for example..
"Net Income" is = to struct.GL numbers (1< & <15), (which change periodically so I don't want to have to change it manually every time I upload a new file).
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日
What are you talking about? Run the xlsread() and your data will be put in the 'a' variable. The code above is just an example showing how it works on a cell array.
Alex
Alex 2011 年 10 月 28 日
Sorry, I just confused myself. This works perfectly. Thank you so much for your help.

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

Honglei Chen
Honglei Chen 2011 年 10 月 27 日

0 投票

Here is a simple example:
>> x = rand(3,3)
x =
0.9055 0.6581 0.2495
0.2045 0.1836 0.3782
0.9654 0.3151 0.5356
>> y = x(x(:,1)>0.5,2)
y =
0.6581
0.3151
HTH
Alex
Alex 2011 年 10 月 28 日

0 投票

Here's an example using the text 'credit' and 'cash' to find and sort out what you want easily.
col_2 = {'credit' 'credit' 'cash' 'cash'};
col_3 = [ 1 2 3 4 ];
%find the credit rows
credit_rows = strcmp(col_2,'credit');
%find indexes of credit row
credit_index = find(credit_rows == 1);
%now we have the index of each credit value, so we can sum these, or move them
only_credit_col = col_3(credit_index);
total_credit = sum(only_credit_col);
%remove the credit cells from the original column - this will leave you a column with only cash values
col_3(credit_index) = [];
cash_col = col_3;
cash_tot = sum(cash_col);
results: credit_col = [1 2] total_credit = 3
cash_col = [3 4] total_cash = 7 total_cash = 7

1 件のコメント

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 28 日
credit_rows is a logic array. It can be used for index. You don't need to generate the credit_index using find()
col_2 = {'credit' 'credit' 'cash' 'cash'};
col_3 = [ 1 2 3 4];
%find the credit rows
credit_rows = strcmp(col_2,'credit');
%now we have the index of each credit value, so we can sum these, or move them
only_credit_col = col_3(credit_rows);
total_credit = sum(only_credit_col);
%remove the credit cells from the original column - this will leave you a column with only cash values
col_3(credit_rows) = [];
cash_col = col_3;
cash_tot = sum(cash_col);

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

カテゴリ

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

質問済み:

2011 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by