How to make M×N matrix in matlab function simulink
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
dpb
le 23 Juin 2022
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?
Réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!