# how to check every single element in a matrix vector wise

111 views (last 30 days)
hafiz hashim on 17 Jul 2019
Commented: hafiz hashim on 17 Jul 2019
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

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

hafiz hashim on 17 Jul 2019
Thank you so much for your help
really appreciated
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