Compare a vector values with martix having multiple rows
6 views (last 30 days)
Show older comments
Muhammad Asad
on 19 May 2022
Commented: Muhammad Asad
on 20 May 2022
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 Comments
Walter Roberson
on 20 May 2022
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?
Accepted Answer
David Hill
on 20 May 2022
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),:);
More Answers (0)
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!