subtract rows under a condition

I am trying to create a data by subtracting rows that meet a condition. A sample of my data is this
dataA =
id value
1 1
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
5 14
5 15
5 16
I have a vector this
my_vector =
1
3
4
1
With data A, I would like to subtract rows that are only included in my_vector. In my_vector, there are two 1 so I would like to subtract rows with id 1 twice. So, here is what I want to have
id value
1 1
1 2
1 3
1 4
3 8
3 9
3 10
4 11
4 12
4 13
1 1
1 2
1 3
1 4

답변 (2개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2018년 5월 4일

0 개 추천

solution:
dataA = [1 1
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
5 14
5 15
5 16];
my_vector =[ 1
3
4
1];
[x,~]=find(bsxfun(@eq, dataA(:,1),my_vector'));
newdataA=dataA(x,:);

댓글 수: 5

Boram Lim
Boram Lim 2018년 5월 4일
Thank you. Since my data is "table", I think it does not work. How should do it with table format?
dta_c1 = dta{:,[1]};
[kk,~]=find(bsxfun(@eq,dta_c1,sampled_id'));
Error using bsxfun Requested 7316521x613023 (4177.2GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
it is what I got. can you help me?
sorry, if is table then :
[x,~]=find(bsxfun(@eq, table2array(dataA(:,1)),my_vector'));
newdataA=dataA(x,:);
Guillaume
Guillaume 2018년 5월 4일
편집: Guillaume 2018년 5월 4일
There is never any raison to use table2array to extract a column from a table, table2array(dataA(:, 1)) is a roundabout way of getting:
dataA{:, 1}
or using . indexing:
data.id
Overall, a simpler version for R2016b or later:
[rows, ~] = find(dataA.id == my_vector');
newdataA = dataA(rows, :);
Boram Lim
Boram Lim 2018년 5월 5일
Since the size of data is too large, there is such an error
Out of memory. Type HELP MEMORY for your options.
Error in Model01_interpolated_May1 (line 70) [rows, ~] = find(dta2.decision_id == sampled_id01');

댓글을 달려면 로그인하십시오.

Guillaume
Guillaume 2018년 5월 6일

0 개 추천

Since the size of data is too large
Then you'll have to use a loop, explicitly or with arrayfun:
newdataA = arrayfun(@(v) dataA(ismember(dataA.id, v), :), my_vector, 'UniformOutput', false);
newdataA = vertcat(newdataA{:})

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

질문:

2018년 5월 3일

답변:

2018년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by