for loop with multiple indices
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ba sa
am 5 Dez. 2021
Beantwortet: Walter Roberson
am 5 Dez. 2021
i want to write a function that goes through each row of a matrix and at the same time go through each element of the first row, what i had in mind was something like this
function X1= RREF_GEN (X,M,N)
for m = 2:M , n = 1:N;
X1=X(m,:)-(X(m,n)/X(1,n))*X(1,:);
end
this doesn't work, and nesting doesn't either because i want the two loops to run at the same time not one after the other
please help
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 5 Dez. 2021
That is not possible for the general case where N is not the same as (M-1) -- that is, if the two variables to be incremented do not have the same length.
If the two did have the same length then
m_vals = 2:M;
n_vals = 1:N;
for idx = 1 : length(m_vals)
m = m_vals(idx);
n = n_vals(idx);
X1=X(m,:)-(X(m,n)/X(1,n))*X(1,:);
end
or
for m = 2 : M
n = m - 1;
X1=X(m,:)-(X(m,n)/X(1,n))*X(1,:);
end
or
for m = 2 : M
X1=X(m,:)-(X(m,m-1)/X(1,m-1))*X(1,:);
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!