How can I run my code for each row of data in my excel spreadsheet?
2 次查看(过去 30 天)
显示 更早的评论
I have a code in MATLAB which works but I want it to run individually for all the rows and produce result for each row. Instead, it runs through the data and gives result for the final row only
4 个评论
dpb
2018-7-5
"We can't debug what we can't see..." Attach the code.
BUT, by your description you're using a loop; in Matlab you should read the data into an array and write your function to operate on the whole array; many times one avoids looping entirely and the result automagically then becomes the desired array.
BUT, "We can't debug what we can't see..." Attach the code.
采纳的回答
Guillaume
2018-7-5
Your R_1, R_2 and R_3 are vectors. Therefore R_1 < 0.1 is a logical vectors, which you're then passing to if. It's almost always a mistake to pass a vector to if and it certainly is in your case. Despite your statement, your code, in fact, does not work. It can only work when R_1, R_2 and R_3 are scalars.
One way you could fix it is to write your if tests into a for loop that iterates over the elements of these 3 vectors simultaneously:
Code = zeros(size(R-1));
for idx = 1:numel(R_1)
r1elem = R_1(idx);
r2elem = R_2(idx);
r3elem = R_3(idx);
%now all your if, testing r1elem, r2elem, r3elem instead of R_1, R_2, R_3
%and assigning to Code(idx) instead of Code
end
I'll point out that you also have made some mistakes in your if testing with for example
if R_3 >1 %probably should have been R_3 < 1
...
elseif (R_3 >= 1 & ...)
Similarly, we find
if (R_2>=0.1 & R_2<3)
...
elseif R_2 > 1 %already taken care by the if for R_2 > 1 & R_2 < 3
Clearly, it is not easy to get such a spaghetti of if/else/elseif correct and thankfully there are much easier ways of obtaining the same result. E.g:
R1_thresholds = [-Inf, 0.1, 1, 3, Inf];
R2_thresholds = [-Inf, 0.1, 1, Inf];
R3_thresholds = [-Inf, 1, 3, Inf];
%create a 3D matrix that tells us which code corresponds to the intersection of each threshold
%R_1 along the rows, R_2 along the columns, R_3 along the pages
%I.E. row 1 of code_by_bin corresponds to R1<0.1, row 2 to R_1>=0.1 & R_1<1, row 3 to R_1>1 & R_1<=3, etc.
%column 1 corresponds to R2<0.1, column 2 to R2>0.1 & R2<1, etc.
%page 1 correspinds to R3<1, etc.
Code_by_bin = cat(3, ... first page for R3<1
[3, 1, 8; ... for R1<0.1 and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2; ... for R1>=0.1 & R1<1
4, 2, 2; ... for R1>= 1 & R1<3, and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2], ... for R1>=3
[7, 7, 8; ... 2nd page for R3>=1 & R3<3, this row for R1<0.1 and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2; ... for R1>=0.1 & R1<1
2, 5, 2; ... for R1>= 1 & R1<3, and R2<1, R2>=0.1 & R2<1, R2>=1 respectively
2, 2, 2], ... for R1>=3
[2, 2, 2; ... 3rd page for R3>=3 everything is 2 regardless of R1 and R2
2, 2, 2;
2, 2, 2;
2, 2, 2]);
%... your excel loading code
R_1 = C_2H_2./C_2H_4;
R_2 = CH_4./H_2;
R_3 = C_2H_4./C_2H_6;
row = discretize(R1, R1_thresholds);
col = discretize(R2, R2_thresholds);
page = discretize(R3, R3_thresholds);
code = Code_by_bin(sub2ind(size(Code_by_bin), row, col, page));
I may have made some errors in the actual code values against bin because of the many mistakes in your if branches. You can fix these easily.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!