How to replace the values of a column of a variable with another column from another variable in matlab?

6 views (last 30 days)
I have two variables 'network' and 'sedsource'. I used the following code to produce the 'input' variable which contains 1 value for the common cells between the two variables and 0 for the others. Now i want to replace these 1 with the 'Volume' field values from the 'sedsource' variable such that the common cells have the volume values while the others are 0. Can anyone please help me with this?
Thank You
b = [network.FID_1];
a = [sedsource.FID_Networ];
Linknum = ismember(b,a);
input = double(Link);

Accepted Answer

Mohammad Sami
Mohammad Sami on 6 Apr 2021
You can do as follows if you want to maintain it as struct array.
b = [network.FID_1];
a = [sedsource.FID_Networ];
[lib,loca] = ismember(b,a);
Vol = cell(size(network));
Vol(lib) = {sedsource(loca(lib)).Volume};
Vol(~lib) = {0};
[network.Volume] = Vol{:};
Alternatively convert your data into a table.
network = struct2table(network);
sedsource = struct2table(sedsource);
[lib,loca] = ismember(network.FID_1,sedsource.FID_Networ);
network.Volume = zeros(height(network),1);
network.Volume(lib) = sedsource.Volume(loca(lib));

More Answers (0)

Community Treasure Hunt

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

Start Hunting!