foor loop that outpus a vector question

4 Ansichten (letzte 30 Tage)
Aroi Mehu
Aroi Mehu am 15 Apr. 2020
Bearbeitet: Aroi Mehu am 15 Apr. 2020
If i have a 4*4 matrix for example. Using for loop, how can i create a function that returns the index (row number which is coloum 1 of the matrix) of the data which specfically is <60 for the 2nd coloum, <20 for the 3rd coloumn and <50 for the 4th coloum.
how would for loop be used in this situation. help would be awesome.

Akzeptierte Antwort

Mehmed Saad
Mehmed Saad am 15 Apr. 2020
x = [1 2 3 4; 70 62 45 65; 23 17 19 20; 51 34 56 72];
cond = [60 20 50];
ind = avada(x,cond)
  1. Initialize false array which is off size [3 4]
  2. Run for loop for 3 iterations
  3. compare 2nd row with condition [70 62 45 65]>=60, then output will be [1 1 0 1]
  4. compare 3rd row with condition [23 17 19 20]>=20, then output will be [1 0 0 1]
  5. compare 4rth row with condition [51 34 56 72]>=50, then output will be [1 0 1 1]
  6. You have all these values in array y i.e = [1 1 0 1;1 0 0 1; 1 0 1 1];
  7. sum column wise output will [3 1 1 3]
  8. values equal to 3, output will be [1 0 0 1]
  9. feed it to x's first row i.e. x(1,[true false false true])
function ind = avada(x,cond)
y = false(size(x)-[1 0]); %iNITIALIZE Y
for i=1:size(x,2)-1 % Run for loop for 3 times i.e. 1 less than 4
y(i,:) = x(i+1,:)>=cond(i);% condition,all values greater than equal to cond will be true
end
ind = x(1,sum(y)== (size(x,2)-1)); %
end

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by