Compare a vector values with martix having multiple rows

5 次查看(过去 30 天)
I want to compare a vector with a matrix having mutiple rows. Exact situation with coding and result described below:
For vector:
a vector of a single row and three columns shown below
coding c = []; c(end+1,:)=[k1 k2 k3]; disp (c);
result 2.4 2.5 3.9
For matrix:
coding b= []; b(end+1,:)=[Kp Ki k3 k4 k5]; disp(b);
result 1 1 2.9 2.7 3.6
1 2 3.5 3.9 3.9
................. so on.
Now, problem is:
Compare matrix column 3, 4 and 5 with a vector (2.4 2.5 3.9). Note, vector values are used as reference. In return, want a result of values in matrix that are equal and less than the values of vector.
In more detail: If any value of column 3 in matrix is equal or less than the 1st value vector then result display. But need a result that import complete row of this matrix. Similarly, if any value of column 4 in matrix is equal or less than the 2nd value vector then result display complete row of this matrix.
All these combinations displayed in a final matrix. Means final matrix contain all reults of import rows find after comparison.
  2 个评论
Walter Roberson
Walter Roberson 2022-5-20
You would potentially have several results for matching column 3, a different number for matching column 4, and so on. Are all of these to go into the same output matrix, without any division between the sections ? Or when you say "display" do you mean to the command window, not storing in a matrix?
Muhammad Asad
Muhammad Asad 2022-5-20
I want to store it in a different output matrix. For example all results after comparision with column 3 goes to a new output matrix e.g. it is B. Simialrly, all results of column 4 goes to a uninque single matrix e.g. matrix D.

请先登录,再进行评论。

采纳的回答

David Hill
David Hill 2022-5-20
yourMatrix=[randi(2,20,2),5*rand(20,3)];
yourVector=5*rand(20,3);
newMatrix=yourMatrix(yourMatrix(:,3)<=yourVector(1)|yourMatrix(:,4)<=yourVector(2)...
|yourMatrix(:,5)<=yourVector(5),:);
  1 个评论
Muhammad Asad
Muhammad Asad 2022-5-20
thanks for sharing your knowledge. I have a question related to this.
If i want to get an output matrix that have values after comparing both values of column 3 and 4rth. If condition meet then display. what should i need to do? I use operator "and" but nothing happened.
For example
compare first value of vector with 3 rd column of matrix and 2nd value with 4rth column of matrix. If both values are less or equal to the values of vector then display entire row of matrix otherwise not.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by