how to check every single element in a matrix vector wise

58 views (last 30 days)
i have a matrix of m x n and i want to apply conditions on each single column
  1. check if any of the value in column is greater then threshold( T1)
  2. then check all the previous values if any value other than the current value is also higher than (T1)
  3. then count the number of values including current value
  4. if count is less than threshold (T2) then replace the current value by 10
looking forward for any help
Thanks

Accepted Answer

David K.
David K. on 17 Jul 2019
A matrix is indexed like this: A(row,column). So to traverse a single column you leave the second value the same while changing the first value. If you want to find all the values in a column larger than a threshold you can do
A(:,col)>T1;
You can easily count them by using sum
sum(A(:,col)>T1);
You can also replace those values easily like this
A(A(:,col)>T1,col) = 10;
In that we are saying to set the values of A in the desired column that are larger than T1 are to be set to ten.
I am not what it is you are saying but here is a way to do what I think you are describing and if it is not quite right, hopefully I have given you what you need to figure out how to do it.
for n = 1:size(A,col)
if A(n,col) > T1 & sum(A(1:n-1,col)>T1)<T2
A(n,col) = 10;
end
end
  2 Comments
hafiz hashim
hafiz hashim on 17 Jul 2019
what i am trying to do is
if let say 5th value of first column is greater than (T1) then it will check all previous values (last 4 values) and if the sum (values greater than T1) is less than (T2) then it will replace the current value (5th value) by 10
Similary, let say if 11th value of first column is also greater than (T1) then it will again check all previous values (last 10 values ) and if the sum (values greater than T1) is less than (T2) then it will replace the current value (11th value) by 10
this is how i want to run it on every single column of matrix A
i hope its understandable now

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!