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

6 次查看(过去 30 天)
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);
Link=Linknum';
input = double(Link);

采纳的回答

Mohammad Sami
Mohammad Sami 2021-4-6
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));

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by