How to solve the error in the following code ''Index Exceeds matrix dimension"?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Manas Ranjan Pattnayak
il 30 Ago 2017
Commentato: Star Strider
il 30 Ago 2017
nt = 100;
nz = 60;
x=linspace(0,2*pi*50,nt);
y=linspace(-80,20,nz);
[X,Y]=meshgrid(x,y);
for i = 1:nt
for j = 1:nz
G(i,j) = 5-(3*cos(X(i,j)/50)); %#ok<SAGROW>
end
end
[n3,n4]=size(G) %#ok<NOPTS>
Why this code generates the dimension as 120 * 120.
Kindly Help.
0 Commenti
Risposta accettata
Star Strider
il 30 Ago 2017
You need to reverse the index assignments:
for i = 1:nz
for j = 1:nt
G(i,j) = 5-(3*cos(X(i,j)/50)); %#ok<SAGROW>
end
end
However you can avoid the loops entirely:
G = 5-(3*cos(X/50));
This produces the same result.
‘Why this code generates the dimension as 120 * 120.’
I cannot reproduce that. When I run it, ‘G’ is (60 x 100).
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!