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)
Show older comments
Celestie Gladys on 25 Jan 2023
Commented: Celestie Gladys on 7 Feb 2023
Idont know where i am going wrong it also displays I when there are no changes ?
for i = (data( : , : ))
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;
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.
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
which is probably not going to be the case very often...
More Answers (1)
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")
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.
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!