Clear Filters
Clear Filters

I have a data 5x148992 , i have to extract data column wise and check if there is any changes wrt previous column and display when there are chnages?

2 views (last 30 days)
Idont know where i am going wrong it also displays I when there are no changes ?
counter=0;
for i = (data( : , : ))
if diff(i)~=0
counter=counter+1;
disp(i);
else
continue;
end
disp(counter)
end
  4 Comments
Celestie Gladys
Celestie Gladys on 25 Jan 2023
yes the data is numeric , even if i did all(diff(i)~=0) the disp(i) chould only show if the two consecutive column are not same , but insted it displays all the consecutive column .
11.500183 11.500183
-0.50000000 -0.50000000
975.51489 975.51489
-28.500427 -28.500427
211.50323 211.50323
Celestie Gladys
Celestie Gladys on 26 Jan 2023
@dpb, i want to print only if the consequtive colmns are different , like in cpp it is, print only if the consequtive colmns are different .
data[i][j]
j=j+1
if [j]`~=[j+1]
printf([j])
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Jan 2023
for i = (data( : , : ))
data appears to be a 2D array.
When you have something of the form
for VARIABLE = EXPRESSION; BODY; end
then that is equivalent to
HIDDEN_INTERNAL_VARIABLE_ORIGINAL = EXPRESSION;
HIDDEN_INTERNAL_VARIABLE_COLUMNS = size(EXPRESSION,2); %COLUMNS
HIDDEN_INTERNAL_VARIABLE_COUNTER = 1;
while HIDDEN_INTERNAL_VARIABLE_COUNTER <= HIDDEN_INTERNAL_VARIABLE_COUNTER
VARIABLE = HIDDEN_INTERNAL_VARIABLE_ORIGINAL(:,HIDDEN_INTERNAL_VARIABLE_COUNTER,:);
HIDDEN_INTERNAL_VARIABLE_COUNTER = HIDDEN_INTERNAL_VARIABLE_COUNTER + 1;
BODY
end
clear HIDDEN_INTERNAL_VARIABLE_ORIGINAL HIDDEN_INTERNAL_VARIABLE_COLUMNS HIDDEN_INTERNAL_VARIABLE_COUNTER
That is, the loop control variable is assigned the columns of the array.
if diff(i)~=0
Your i would contain an entire column out of data . diff() applied to the column column would (generally) be a column vector. You are then testing whether the column vector == 0, which will calculate a column vector of results.
When if is applied to a non-scalar, the condition is considered to be true if all of the values are non-zero, so that test would be equivalent to
if all(diff(i)~=0)
which is probably not going to be the case very often...

More Answers (1)

dpb
dpb on 26 Jan 2023
Edited: dpb on 27 Jan 2023
" i want to print only if the conse[c]utive col[u]mns are different ..."
Well, then there should not be a disp statement outside the test condition; as noted above, you've got the other one where it is executed every pass regardless the condition test.
As @Walter Roberson and I both explained (and as is documented at <for>, the MATLAB vectorized loop construct passes the variable by column as the loop index variable. This is uniquely different behavior than other non-vectorized languages with which you may be familiar.
What you're looking for here in loop construction would then be something more like
nc=size(data,2); % number of data array columns
for i=2:nc % iterate over the columns beginning w/ second
if all(data(:,i)==data(:,i-1)) % see if column i==column i-1
disp("Column "+i+" matches previous column")
end
end
However, "the MATLAB way" to do the above would use the vector capabilities of MATLAB something more like
ix=find(~all(diff(data,1,2)))+1; % find all columns match previous
disp("Column "+ix(:)+" matches previous column") % disp() all those to screen
where ix would contain the index to all columns in the original array for which that column matched the previous.
NB: Neither of the above finds repeated columns throughout the array, only subsequent columns that are identical.
NB2nd: There's implied issue in above comparison of floating point values that even one rounded bit of precision will return FALSE -- if values of these columns are computed by alternate routes, even simply the order of numeric operations, then two values that are nominally the same may fail in the test. If this can possibly be an issue here, then see ismembertol for comparisons with tolerances.

Categories

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

Community Treasure Hunt

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

Start Hunting!