I want to do element comparison between two matrices

3 ビュー (過去 30 日間)
Hems
Hems 2016 年 8 月 2 日
編集済み: Azzi Abdelmalek 2016 年 8 月 2 日
I want to do element comparison between two matrices and if the second element is less than or equal to the previous one it should return that element as result of that matrix...for example 1 st matrix A=[1 2 3 4 5 6]; second Matrix B=[0.1 1 1.5 4.5 4 2.5 2 4]; every element of B has to be checked with A which is in ascending order and when the element of is < or = element of A that should return that value or else should go to check for next element. As a result it shall produce Result=[1 1 2 5 4 3 2 4]. I tried writing with for loop I get error dimension of two input matrix must match which is not in my case. Please help me out. TIA!
  1 件のコメント
Hems
Hems 2016 年 8 月 2 日
編集済み: Azzi Abdelmalek 2016 年 8 月 2 日
A=[1 2 3 4 5 6];
B=[0.1 1 1.5 4.5 4 2.5 2 4];
for i=1:max(size(B))
for j=1:max(size(A)-1)
if B(1,i)<=A(1,j)
R(1,i)=A(1,j);
break
else
R(1,i)=A(1,j+1);
end
end
end
It works !!! but is there way to make it very fast I have more than a million elements to compare.

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 2 日
A=[1 2 3 4 5 6];
B=[0.1 1 1.5 4.5 4 2.5 2 4]
BB=permute(B,[1 3 2])
C=bsxfun(@ge ,A,BB)
for k=1:numel(B)
out(k)=A(find(C(:,:,k),1))
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by