How to make M×N matrix in matlab function simulink
2 次查看(过去 30 天)
显示 更早的评论
I make this code in matlab function simulink.
I set a sampling time and a simulation time, 0.001s and 10.0s.
I wanted a parameter, p(10001×100)(time×H), whose 100 is a roop number.
However, the parameter p was (1000100×100). The row was also set 100 times, like a double loop.
How can i fix them in simulink?
Below is the code in matlab function simlink.
The paramater input is (10001×1).
function p = fcn(input)
H=100;
dh=0.1;
p=zeros(H);
for r=1:1:H
p(r)=r*dh;
end
end
2 个评论
dpb
2022-6-23
p=zeros(H);
creates a 2D array of zeros HxH. Is that what was intended/wanted?
Your function doesn't ever use the argument variable input?
The loop as written will execute 100 times and set the linear addressing element r (1:100) only; since MATLAB is column-storage major, that will set the first column of p and leave the other 99 as zeros.
That result could be more efficienty be code in MATLAB using array syntax as
function p = fcn(input)
H=100;
dh=0.1;
p=dh*[1:H];
end
which still doesn't use the argument variable so also is probably not what is intended.
It's not clear to me from the description the exact result wanted; can you use a much smaller size of arrays (say 10 elements or so instead of 10001) and show an actual input and what the output would be for it?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!