필터 지우기
필터 지우기

loop or other better solution for mixing?

조회 수: 1 (최근 30일)
addy fang
addy fang 2020년 7월 27일
편집: per isakson 2020년 7월 28일
Hello, I want to do some mixing and wonder if there is any better solution other than looping.
I want to mix any number of ingredients to make one solution. The total percentage of the ingredients added up should equal to 100%. The percentage of each ingredient can be from 0% to 100%. I need to go through and save all the possible compositions with distinct names, and do calculations in order to find out the best composition.
Starting with two databases A and B, each with 4 columns of numerical data. I will need to produce and save all the mixed combination. For example, 10% of each column in A add 90% of the corresponding column in B, and save the result. The total percentage is 100%. This could be done with loop. x=0:0.1:1, C=x*A+(1-x)*b. This seems straightforward, if I only have two databases A and B.
If I have three databases, A, B and C, to produce D. So I need two loops?
If I have A, B, C and D, to produce E, so I need to have three loops?
If I want to mix more, do I have to create another loop again?
As the number of the databases I want to mix increase, this looping solution seems inefficient. Because everytime the numer of database changes, I need to rewrite my code again.
I was wondering if there is any better solution for this problem. So it does not need me to redo the coding even if I change the number of databases to be mixed. I hope I only need one code to run whatever number of databases that I want to mix.
I tried the following for two components and it did not work:
data1=importdata('\rawdata1.txt');
data2=importdata('\rawdata2.txt');
for x = 0:10:100, y = 0:10:100 & x+y=100;
data = data1.*x + data2.*y;
folder = '\Desktop\';
filename = sprintf('data_component1_%d_component1_%d.xls', x, y);
fullfilename = fullfile(folder, filename);
xlswrite(fullfilename, data);
end
Can someone help? Thank you very much.
  댓글 수: 2
Vladimir Sovkov
Vladimir Sovkov 2020년 7월 27일
Write matlab function with the "varargin" argument. See matlab documentation on it.
addy fang
addy fang 2020년 7월 28일
편집: addy fang 2020년 7월 28일
Hi Vladimir, Can you write an example solution for this problem?
I don't know how to do it. I am a newbee. Thank you.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by