for loop does not iterate
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Konrad Suchodolski
el 21 de Nov. de 2023
Comentada: Voss
el 21 de Nov. de 2023
I am trying to get used to matlab, but I am stuck.
I'm not able to iterate through my x_roots and separate real roots form complex ones
This is my code:
clear all
close all
syms n
w = @(x) 0.01.*x.^4-0.04.*x.^3+0.02.*x.^2+0.04.*x-0.03;
w_vec = [0.01 0.04 0.02 0.04 0.03];
x = -2:.2:4;
w_y = w(x);
x_roots = roots(w_vec)
x_realroots = [];
for i=x_roots
~round(imag(i))
if (~round(imag(i)))
round(imag(i))
x_realroots = [x_realroots i];
else
continue;
end
end
x_realroots
So yeah, I just wanted to get real roots, but now I'm learning basic matlab.
Please explain to me why i cannot iterate through x_roots.
0 comentarios
Respuesta aceptada
Voss
el 21 de Nov. de 2023
Editada: Voss
el 21 de Nov. de 2023
"Please explain to me why i cannot iterate through x_roots."
A for loop in MATLAB iterates over the columns of what you give it. Here are some simple examples to illustrate what I mean:
Example 1: row vector
x = 1:3 % a row vector with 3 elements (i.e., 3 columns)
count = 0;
for ii = x % for loop iterates 3 times
count = count+1;
disp("Iteration: " + count)
disp("ii = ")
disp(ii) % ii is a column of x each time (each column of x is a single element)
end
Example 2: matrix:
x = magic(3) % a matrix with 3 columns
count = 0;
for ii = x % for loop iterates 3 times
count = count+1;
disp("Iteration: " + count)
disp("ii = ")
disp(ii) % ii is a column of x each time
end
Example 3: column vector:
x = [1;2;3] % a column vector with 3 elements
count = 0;
for ii = x % for loop iterates 1 time (x has only one column)
count = count+1;
disp("Iteration: " + count)
disp("ii = ")
disp(ii) % ii is the entirety of x
end
In your code your variable x_roots is a column vector, so your for loop iterates one time, and in that iteration i is equal to x_roots.
clear all
close all
syms n
w = @(x) 0.01.*x.^4-0.04.*x.^3+0.02.*x.^2+0.04.*x-0.03;
w_vec = [0.01 0.04 0.02 0.04 0.03]
x = -2:.2:4;
w_y = w(x);
x_roots = roots(w_vec) % x_roots is a column vector ...
count = 0;
for i=x_roots % ... so the for loop iterates once
count = count+1;
disp("Iteration: " + count)
disp("i = ")
disp(i)
end
But you want to iterate over each element of x_roots, not over each column of x_roots. An easy way to do that is to tranpose x_roots in the for expression so that what the for sees is a row vector, in which case it iterates once for each element:
x_realroots = [];
for i=x_roots.' % x_roots.' is a row vector, so the loop iterates multiple times
~round(imag(i))
if (~round(imag(i)))
round(imag(i))
x_realroots = [x_realroots i];
else
continue;
end
end
x_realroots
A more general solution, which would work whether x_roots is a row vector or a column vector (or indeed an array of any size or dimensionality), would be, again in the for expression, convert x_roots into a column vector using (:) notation and then transpose to produce a row vector of all the elements of x_roots:
for i = x_roots(:).' % iterates over the elements of x_roots, regardless of its size
% ...
end
2 comentarios
Más respuestas (2)
madhan ravi
el 21 de Nov. de 2023
Editada: madhan ravi
el 21 de Nov. de 2023
x_realroots = x_roots(abs(imag(x_roots)) < 1e-4) % 1e-4 tolerance
0 comentarios
Les Beckham
el 21 de Nov. de 2023
Array indices in Matlab must be positive integers (or logical). You are trying to us xroots, a vector of complex numbers as the index in your for loop. Loop using integers instead and use that index to select each element of x_roots.
clear all
close all
syms n
w = @(x) 0.01.*x.^4-0.04.*x.^3+0.02.*x.^2+0.04.*x-0.03;
w_vec = [0.01 0.04 0.02 0.04 0.03];
x = -2:.2:4;
w_y = w(x);
x_roots = roots(w_vec)
x_realroots = [];
% for i=x_roots
for i = 1:numel(x_roots)
% ~round(imag(i))
% if (~round(imag(i)))
if (imag(x_roots(i)) == 0)
% round(imag(i))
% x_realroots = [x_realroots i];
x_realroots = [x_realroots real(x_roots(i))];
else
continue;
end
end
x_realroots
x_realroots2 = real(x_roots(imag(x_roots) == 0))
Note that you don't even need a loop.
Also, if you are just getting started with Matlab, I would highly recommend that you take a couple of hours to go through the free online tutorial: Matlab Onramp
3 comentarios
Dyuman Joshi
el 21 de Nov. de 2023
Editada: Dyuman Joshi
el 21 de Nov. de 2023
"You are trying to us xroots, a vector of complex numbers as the index in your for loop."
@Les Beckham, For loop index values can be anything. Do not confuse them with array indices.
And OP is not trying to use the for loop indices as array indices.
Les Beckham
el 21 de Nov. de 2023
Good point. I had misinterpreted what they were trying to do. Of course, the real "Matlab way" is to not have a loop in the first place.
w_vec = [0.01 0.04 0.02 0.04 0.03];
x_roots = roots(w_vec)
x_realroots2 = real(x_roots(imag(x_roots) == 0))
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!