Compare column vector to matrix

Hello,
I'm trying to find the first element and its index into a 73X615 matrix, where each value should be less or equal to a value in 73X1 column vector. So let's say that the matrix is
B = -5 + (5+1)*rand(73,615);
and the vector is
A = -5 + (5+1)*rand(73,1)
My intention is to extract a columns vectors [C, iC], where each value of C is the first value in B that is less or equal than the value in A at the corresponding row, and iC is the index of that value.
My apologies if the question seems a bit contrived, but I'm new to Matlab and I didn't know how to formulate it more clearly.
I hope you can help me with this issue, before hand, many thanks.

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 5 日
編集済み: Azzi Abdelmalek 2016 年 8 月 5 日

0 投票

B = -5 + (5+1)*rand(73,615)
A = -5 + (5+1)*rand(73,1)
id=bsxfun(@le,B,A)
for k=1:size(id,1)
ii=find(id(k,:),1)
out(k,1:2)=[B(k,ii) ii]
end
Incase there are empty indices
B = -5 + (5+1)*rand(73,615)
A = -5 + (5+1)*rand(73,1)
id=bsxfun(@le,B,A)
kk=0
for k=1:size(id,1)
ii=find(id(k,:),1)
if ~isempty(ii)
kk=kk+1
out(kk,1:2)=[B(k,ii) ii]
end
end
out

7 件のコメント

Simon
Simon 2016 年 8 月 5 日
Many thanks for the quick answer. I'll try this asap.
Simon
Simon 2016 年 8 月 5 日
Sorry, I had a typo on my question, I mean: "...to extract two column vectors...". So the code works great, but I don't know how to separate the output into two different column vectors. Thanks again.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 5 日
c1=out(:,1)
c2=out(:,2)
Simon
Simon 2016 年 8 月 5 日
Ok, great, thank you.
Simon
Simon 2016 年 8 月 5 日
編集済み: Simon 2016 年 8 月 5 日
Sorry for bothering again. I was trying to figure out how to find the last value lesser or equal to A, but I couldn't (I used the direction 'last', but I don't know if that's correct for this case). What I'm trying to do now is to determine a range between the first and las value that are less or equal to A. But I need to keep the values in between the first and last points even if they are greater than A.
Another problem I found is that when I try to add a time vector for indexing the matrix like: iT = 1:length(sInput.TimeVector); the out step gives me the error of "mismatching subscript dimensions". Probably I'm adding the time vector wrongly thusly: B = B(:,iT,:), [], 2; I've tried using sort as well: B = sort(B(;,iT,:), 2), which shows a matrix of values: 73 129. But, the error still appears.
Sorry for the long comment. And many thanks.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 5 日
Simon, this is not how it works in this forum, when an answer satisfies your need, you have to mark the answer as accepted. You can also ask for some explanations related to your question, if it's not you have to post another question.
Simon
Simon 2016 年 8 月 5 日
I'm sorry, it's my first time using the forum. I'll open other question.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

質問済み:

2016 年 8 月 5 日

コメント済み:

2016 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by