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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Celestie Gladys
am 25 Jan. 2023
Kommentiert: Celestie Gladys
am 7 Feb. 2023
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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...
Weitere Antworten (1)
dpb
am 26 Jan. 2023
Bearbeitet: dpb
am 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.
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!