Pick up gropu of values in multiple arrays
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Fifteen12
2022-12-9
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?
回答(1 个)
Rohit
2022-12-27
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);
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:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!