Compare vector to two other vectors
2 ビュー (過去 30 日間)
古いコメントを表示
I'm struggling with the following binning task:
I have three vectors of different sizes k, m and n. My goal is to get an
-matrix in which the
-th entry simply counts how many elements from the k-vector are simultaneously smaller than both
and
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1319020/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1319025/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1319030/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1319035/image.png)
So basically I want to do something like
but with one of the inputs as a matrix rather than a vector.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1319040/image.png)
Is there an efficient way to do this?
Of course I can and have also done it with a loop, but that seems terribly inefficient especially since the k from my simplified example is actually really large. Trying to write everything in
-Matrices results in too large arrays unfortunately.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1319045/image.png)
Thanks in advance!
1 件のコメント
Dyuman Joshi
2023 年 3 月 9 日
"Of course I can and have also done it with a loop..."
Can you show what have you tried?
Also, please attach your data using the paperclip button. It will be helpful in providing an answer to your problem.
採用された回答
その他の回答 (2 件)
David Hill
2023 年 3 月 9 日
m=randi(100,1,20);
n=randi(100,1,30);
k=randi(100,1,10000);
[M,N]=meshgrid(m,n);
x=min(M,N);
z=zeros(size(x));
u=unique(x);
y=sum(k<u,2);
for j=1:length(u)
z(x==u(j))=y(j);
end
0 件のコメント
Steven Lord
2023 年 3 月 9 日
I think what you want is to use histcounts2 to calculate how many numbers fall into each bin between m(k) and m(k+1) [and between n(k) and n(k+1)] then call cumsum twice, once to sum across the rows and once to sum the matrix from the previous call down the columns.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!