Compare a vector values with martix having multiple rows

2 visualizaciones (últimos 30 días)
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 comentarios
Walter Roberson
Walter Roberson el 20 de Mayo de 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?
Muhammad Asad
Muhammad Asad el 20 de Mayo de 2022
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.

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 20 de Mayo de 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),:);
  1 comentario
Muhammad Asad
Muhammad Asad el 20 de Mayo de 2022
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by