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

6 Ansichten (letzte 30 Tage)
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);

Akzeptierte Antwort

Mohammad Sami
Mohammad Sami am 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));

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by