Pick up gropu of values in multiple arrays

1 view (last 30 days)
Hi,
I have a data array like this.
[X=B T A
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 0 1
0 0 1
0 1 0
1 0 0
1 0 0];
In this I have to pick up the values from group sum of X(:,1)=[10 2];
X(:,2)=[7 1]; X(:,3)=[2];
Now I need to pick up the triad=[10 7 2 ]; Bold values
And with the condition X(:,1) groupsum >=3 then only consider that .
Also, consider B (groupsum of >=3) next corresposning T (doesnt have any condition) and T corresponding A(gropu sum can be >1)
Thanks,
  5 Comments
Fifteen12
Fifteen12 on 9 Dec 2022
Unfortunately I'm still not following. You say you need to pick values from B where the group sum is >= 3, can you explain what you mean? You can pick values from B, are you saying you need to pick a random assortment of values until that collected set has a group sum greater than or equal to 3?
Poorna Durga Geesupalli
Poorna Durga Geesupalli on 9 Dec 2022
Hi, john
I rearranged my data for your better understading. Now, data consits of single column.
Please find the excel sheet atttached here. Follow only sheet 2 values

Sign in to comment.

Answers (1)

Rohit
Rohit on 27 Dec 2022
Hi,
I am adding a sample example below which has a 2D array ‘x’ (you can create this from the 'XLSX' file) and find the sum of ones in a consecutive group with the constraint of >=min (the min value) using the “givegroupsum()” function. I assume that ‘x’ only contains zeros and ones. You can take it as a reference and modify it based on your use case.
x=[[1 0 0]
[1 0 0]
[1 0 0]
[1 0 0]
[0 0 0]
[1 0 0]
[1 0 0]
[1 0 0]
[1 0 0]
[1 0 0]
[0 1 0]
[0 1 0]
[0 1 0]
[0 1 0]
[0 1 0]
[0 1 0]
[0 1 0]
[0 0 1]
[0 0 1]
[0 1 0]
[1 0 0]
[1 0 0]];
result = givegroupsum(x(:,1),3);
disp(result);
4 5
function groupsum = givegroupsum(arr,min)
if(nargin<2)
min=1;
end
countgrp=0;
consec=0;
conseccount=0;
for i=1:length(arr)
if(~consec && arr(i))
countgrp=countgrp+1;
end
if(arr(i))
conseccount=conseccount+1;
end
if(consec && ~arr(i))
if(conseccount<min)
countgrp=countgrp-1;
end
end
%updating values based on currect item
consec=arr(i);
if(~arr(i))
conseccount=0;
end
end
if(consec && conseccount<min)
countgrp=countgrp-1;
end
groupsum=zeros(countgrp,1);
consec=0;
conseccount=0;
j=1;
for i=1:length(arr)
if(arr(i))
conseccount=conseccount+1;
end
if(consec && ~arr(i))
if(conseccount>=min)
%adding the value to results array
groupsum(j)=conseccount;
j=j+1;
end
end
consec=arr(i);
if(~arr(i))
conseccount=0;
end
end
if(consec && conseccount>=min)
groupsum(j)=conseccount;
end
end
You can refer to the below documentation of 'readmatrix' to read the data from 'XLSX' file:

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!