How can I selectively multiplying the negative values with (-1) in an excel file?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Star Strider
2023년 11월 11일
편집: Star Strider
2023년 11월 11일
Multiplying the negative values of column 2 by -1 will of course create them as positive values for all the entries.
댓글 수: 0
dpb
2023년 11월 11일
편집: dpb
2023년 11월 11일
data=readmatrix('yourExcelfile.xlsx');
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=readmatrix('yourExcelfile.xlsx');
data(:,2)=abs(data(:,2));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!