how to compare two consecutive values in a matrix
14 views (last 30 days)
how to compare two consecutive values in a matrix called 'temp' row-wise, the rule and the expecting result is as the following:
if first value>=second value , then bit=1;
else if first value<second value, then bit=0;
Store the bit generated every time in a matrix ‘M’.
This step will generate a matrix ‘M’ (logical) of size same a temp.
say for instance let's say matrix 'temp' be temp=[1,2,2,1;3,4,4,3;5,6,6,5;7,8,8,7]
can anyone give me some hints, thx a lot !
Voss on 29 Jul 2022
In each row, you have one fewer comparisons than you have elements, so M is of size one less than temp.
M = logical(temp(:,1:end-1) >= temp(:,2:end))
Maybe you want to append a column of false values to M.
M(:,end+1) = false
Another way to do the same:
temp2 = [temp NaN(size(temp,1),1)];
M = logical(temp2(:,1:end-1) >= temp2(:,2:end))