How to perform individual operation on a Column of data from excel file?

2 ビュー (過去 30 日間)
Khondokar Fida Hasan
Khondokar Fida Hasan 2017 年 10 月 23 日
コメント済み: Walter Roberson 2017 年 10 月 23 日
I am intended to perform a separate operation (adding/subtracting some values) on Positive and Negative value from an Excel dataset using Matlab. To read the column I use the following code
num = xlsread('testdata.xls', 'B:B');
it reads fine and when I ask 'num' it shows the data. However, the data are having positive and negative values. I want to add some values to negative dataset and substruct from the positive dataset by keeping their position alike so that I can reconstruct a graph having exactly the same pattern. I have made two attempts:
1. Using logical indexing I can separate the positive and negative values and then perform the operation but can't combine them in the same order, something like following
idx = num<0;
neg = num(idx)+ 1.0e-08
pos = num(~idx)- 1.0e-08
2. If-else also not working at all:
if (num<0)
num = num +1
elseif(num>0)
num = num +2
end
Any help, please?

採用された回答

KSSV
KSSV 2017 年 10 月 23 日
編集済み: KSSV 2017 年 10 月 23 日
idx = num<0;
num(idx) = num(idx)+ 1.0e-08
num(~idx)= num(~idx)- 1.0e-08

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 23 日
idxneg = num<0;
num(idxneg) = num(idxneg)+ 1.0e-08;
idxpos = num>0;
num(idxpos) = num(idxpos)- 1.0e-08;
I did not use ~idxneg because ~idxneg would include num == 0 exactly, but exact zeros are not positive values.
There is a shortcut for this code:
num = num - sign(num) * 1.0e-08;
This even leaves 0 unchanged.
  2 件のコメント
Khondokar Fida Hasan
Khondokar Fida Hasan 2017 年 10 月 23 日
This shortcut seems wonderful but in my case the operation (adding value) is different. Though in aforementioned case I use 1.0e-08 for every case, but I want to perform different adjustment for a different case.
Walter Roberson
Walter Roberson 2017 年 10 月 23 日
offsets = [1.0e-08, 0, -8.7e-9];
num = num + offsets(2 + sign(num));
provided that the breakpoints are still negative, 0, positive

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by