# How can I selectively multiplying the negative values with (-1) in an excel file?

1 visualizzazione (ultimi 30 giorni)
Md Akibul il 11 Nov 2023
Modificato: dpb il 11 Nov 2023
Hi everyone, I'm trying to read an excel file which has two columns and I want to multiply the negative values of column 2 with (-1). Could anyone please help me with a code to do that? Thank you!
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

Star Strider il 11 Nov 2023
Modificato: Star Strider il 11 Nov 2023
Multiplying the negative values of column 2 by -1 will of course create them as positive values for all the entries.
Use the abs function on column 2 to do that.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

dpb il 11 Nov 2023
Modificato: dpb il 11 Nov 2023
isneg=(data(:,2)<0);
data(isneg,2)=-data(isneg,2);
"Use the Force, Luke!" Here "the Force" is logical indexing, one of the most powerful features in MATLAB.
That is, of course, if you really want/need to negate a specific subset; as @Star Strider notes, for the specific operation it is the equivalent of taking the absolute value of the column in which case you can save the extra step of the indexing expression --
data(:,2)=abs(data(:,2));
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Spreadsheets in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by