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!