Writing an algorithm into Matlab syntax
1 次查看(过去 30 天)
显示 更早的评论
Hello all,
I am trying to write the following algorithm into Matlab. Can someone please help me with putting this into Matlab syntax? Thanks. Lovinash
countA = 0
countB = 0
countC = 0
Import "C:\\Test.xlsx", "Sheet1", "A1:B730" as an array or matrix called 'L'
Define A as an array or matrix
Define B as an array or matrix
Define C as an array or matrix
for i from 1 to 99 do
x := L[i, 1]; (X = value in row i and column 1 of L)
y := L[i, 2]; (Y = value in row i and column 2 of L)
if 0<= y < 25 then
countA = countA+1
Include x and y to Array/Matrix 'A'
else if 25<= y <60 then;
countB := countB+1;
Include x and y to Array/Matrix 'B'
else if 60<= y then;
countC := countC+1
Include x and y to Array/Matrix 'C'
end if;
end do :
Print countA;
Print countB;
Prinnt countC;
Export array A to file "C:/Maple/A.xlsx" (and create the file)
Export array B to file "C:/Maple/B.xlsx" (and create the file)
Export array C to file "C:/Maple/C.xlsx" (and create the file)
1 个评论
Jan
2012-9-9
Why didn't you try to implement this by yourself? If your argument is, that you do not know Matlab, posting code here would have an important drawback: You would not be able to debug the code. Therefor I'd suggest to learn Matlab a little bit, try to implement your program, post it here and use the suggestions to improve your code and your Matlab skills.
回答(1 个)
Rick Rosson
2012-9-7
编辑:Rick Rosson
2012-9-7
Here is a start:
count = zeros(3,1);
L = xlsread('C:\\Test.xlsx','Sheet1','A1:B730');
N = 99;
x = L(1:N,1);
y = L(1:N,2);
idx = ( 0 <= y & y < 25 );
count(1) = sum(idx);
A = [ x(idx) y(idx) ];
Does that help? Do you understand what each line of code is doing? Can you figure out how to do the rest on your own?
To print the results, please review:
>> doc disp
and
>> doc fprintf
To export the data to Excel, please review:
>> doc xlswrite
Rick
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!