How to replace all the values of a variable in a dataset following a condition

2 ビュー (過去 30 日間)
Renzo Lanfranco
Renzo Lanfranco 2019 年 1 月 12 日
コメント済み: Renzo Lanfranco 2019 年 1 月 13 日
Hi all. I have a dataset 'data' (16 x 70 fields), which is a structure. Each field has 14 vectors, 1 with strings (1 value) and the rest with integers (1 value per field). I want to replace all the values of one of those vectors for each field (let's call it VECTOR) for the whole data set following the criteria: if it's 0, replace it with 1, if it's 1, replace it with 0. I've tried this:
data(data.VECTOR == 1) = 0;
data(data.VECTOR == 0) = 1;
But it doesn't work. Any ideas?
By the way, there are only three possible values: -1, 0, 1. I want to replace the 0 with 1, and the 1 with 0, while leaving the -1 unchanged.
  12 件のコメント
Walter Roberson
Walter Roberson 2019 年 1 月 12 日
編集済み: Walter Roberson 2019 年 1 月 12 日
Another approach if your original values are only -1, 0, 1:
map = [-1, 1, 9]; %new values for -1, 0, 1
for idx = 1 : numel(data)
data(idx).sideReport = map(data(idx).sideReport + 2);
data(idx).sideAccu = map(data(idx).sideAccu + 2);
end
adding 2 transforms the -1, 0, 1 (not valid indices) to [1 2 3], which can be used in a lookup table.
Renzo Lanfranco
Renzo Lanfranco 2019 年 1 月 13 日
Thanks so much, Walter. This solution worked perfectly:
mask0 = data(idx).sideReport == 0;
mask1 = data(idx).sideReport == 1;
data(idx).sideReport(mask0) = 1;
data(idx).sideReport(mask1) = 0;

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeAuthor Block Masks についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by