Hello,
i have the following two tables:
Tab1=table('Size',[9 3],'VariableTypes',{'cell','double','double'},'VariableNames',{'Description','Year','Value'});
Tab1.Description(:)={'Gas','Gas','Gas','Pellets','Pellets','Pellets','Oil','Oil','Oil'};
Tab1.Year(:)=[2015,2020,2025,2015,2020,2025,2015,2020,2025];
Tab2=table('Size',[6 3],'VariableTypes',{'cell','double','double'},'VariableNames',{'Description','Year','Value'});
Tab2.Description(:)={'Wood','Wood','Wood','FW','FW','FW'};
Tab2.Year(:)=[2015,2020,2025,2015,2020,2025];
Tab2.Value(:)=[5,10,17,7,25,75];
I try the following calculation:
Tab1(strcmp(Tab1.Description,or('Gas','Oil')),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value');
The or-part seems to be the problem. I want the Value of 'FW' from Tab2 as the Value of 'Gas' and 'Oil' in Tab1. My original table is way bigger, so seperate calculations like:
Tab1(strcmp(Tab1.Description,'Gas'),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value');
Tab1(strcmp(Tab1.Description,'Oil'),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value');
are not purposeful. Maybe an if-condition with
||
could help, but i dont know how.
I will greatly appreciate any assistance.

댓글 수: 3

madhan ravi
madhan ravi 2019년 4월 15일
Explicitly state how your output should look like.
Hello madhan ravi,
the output should be the same as with the following calculation:
Tab1(strcmp(Tab1.Description,'Gas'),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value');
Tab1(strcmp(Tab1.Description,'Oil'),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value');
Table looks like:
Description Year Value
_________________________________________
'Gas' 2015 7
'Gas' 2020 25
'Gas' 2025 75
'Pellets' 2015 0
'Pellets' 2020 0
'Pellets' 2025 0
'Oil' 2015 7
'Oil' 2020 25
'Oil' 2025 75
Max Bornemann
Max Bornemann 2019년 4월 16일
Can someone help?

댓글을 달려면 로그인하십시오.

 채택된 답변

Ben Cunningham
Ben Cunningham 2019년 4월 16일
편집: Ben Cunningham 2019년 4월 16일

0 개 추천

Generally I would recommend just writing out two lines as Adam proposed earlier.
But if you really want 'one line' with a list of strings to index then you could wrap it in a for loop :
for DescriptionIndex = {'Gas', 'Oil'}, Tab1(strcmp(Tab1.Description, DescriptionIndex),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value'); end

댓글 수: 5

Max Bornemann
Max Bornemann 2019년 4월 16일
Thank you Ben Cunningham! The for loop is the perfect solution to my problem :-)
Ben Cunningham
Ben Cunningham 2019년 4월 16일
Cheers ;)
I just have a further question, Mr. Cunningham.
Lets say i have the following tables:
Tab1=table('Size',[5 2],'VariableTypes',{'cell','double'},'VariableNames',{'Description','Value'});
Tab1.Description(:)={'Gas','Pellets','Oil','Electricity','Heatpump'};
Tab1.Value(:)=[5,10,17,7,25];
Tab2=table('Size',[7 2],'VariableTypes',{'cell','double'},'VariableNames',{'Description','Value'});
Tab2.Description(:)={'Districtheat','Coal','Gas','Pellets','Oil','Electricity','Heatpump'};
Tab3=table('Size',[4 2],'VariableTypes',{'cell','double'},'VariableNames',{'Description','Value'});
Tab3.Description(:)={'Wood','NaturalGas','HeatingOil','Heat'};
Tab3.Value(:)=[1,2,3,4];
This is the calculation i want to do:
for Sum2={'Wood','HeatingOil','Heat'}
for Sum1={'Pellets','Oil','Heatpump'}
Tab2{strcmp(Tab2.Description,Sum1),'Value'}=Tab1{strcmp(Tab1.Description,Sum1),'Value'}+Tab3{strcmp(Tab3.Description,Sum2),'Value'};
end
end
Is there a way to calculate both for-Loops at the same time? So calculation of entry 1 of Sum1 with entry 1 of Sum2 and so on.
So the 'long' way for what i want would be:
Tab2{strcmp(Tab2.Description,'Pellets'),'Value'}=Tab1{strcmp(Tab1.Description,'Pellets'),'Value'}+Tab3{strcmp(Tab3.Description,'Wood'),'Value'};
Tab2{strcmp(Tab2.Description,'Oil'),'Value'}=Tab1{strcmp(Tab1.Description,'Oil'),'Value'}+Tab3{strcmp(Tab3.Description,'HeatingOil'),'Value'};
Tab2{strcmp(Tab2.Description,'Heatpump'),'Value'}=Tab1{strcmp(Tab1.Description,'Heatpump'),'Value'}+Tab3{strcmp(Tab3.Description,'Heat'),'Value'};
Found the solution to my problem :-)
for Sum1={'Pellets','Oil','Heatpump';'Wood','HeatingOil','Heat'}
Tab2{strcmp(Tab2.Description,Sum1(1,:)),'Value'}=Tab1{strcmp(Tab1.Description,Sum1(1,:)),'Value'}+Tab3{strcmp(Tab3.Description,Sum1(2,:)),'Value'};
end
Well found!
If you are doing this mulitiple times you might consider putting this code in a nicely named function which may help it look clearer.
(Name the function something that makes sense for what you are doing - also the variables might benefit from clearer names).
e.g. At the end of your script :
function TabOut = myFunctionName (Tab1, Tab2, Tab3, Sum)
% My Function does something
for Sum1 = Sum
Tab2{strcmp(Tab2.Description,Sum1(1,:)),'Value'}=Tab1{strcmp(Tab1.Description,Sum1(1,:)),'Value'}+Tab3{strcmp(Tab3.Description,Sum1(2,:)),'Value'};
end
TabOut = Tab2;
end
Then in your script you can write :
Tab2 = myFunctionName (Tab1, Tab2, Tab3, {'Pellets','Oil','Heatpump';'Wood','HeatingOil','Heat'})

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 4월 16일
편집: Adam Danz 2019년 4월 16일

1 개 추천

You would use ismember() to find the rows of Tab1.Description that match a list of options.
ismember(Tab1.Description, {'Gas', 'Oil'})
However, you'll find out that this matches 6 rows but the data on the right hand side of the equal sign only produces 3 rows.
What I think you're trying to do is to assign those 3 values to all rows in Tab1 that are labeled 'Gas' and to assign those 3 values to all rows labeled 'Oil', too.
Tab1(strcmp(Tab1.Description,'Gas'),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value');
Tab1(strcmp(Tab1.Description,'Oil'),'Value')=Tab2(strcmp(Tab2.Description,'FW'),'Value');

댓글 수: 3

Max Bornemann
Max Bornemann 2019년 4월 16일
Thank you Adam Danz! I think the solution of Ben Cunningham with the for loop solves my problem best.
Adam Danz
Adam Danz 2019년 4월 16일
Nice! Glad it worked out. My advice is to write the for-loop using separate lines rather than forcing it into a single line. It doesn't change the speed of performance - it's just more readable as separate lines.
Ben Cunningham
Ben Cunningham 2019년 4월 16일
Aye that's right.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2019년 4월 15일

댓글:

2019년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by