is it possible to use "for loop" for matrix?

1 visualización (últimos 30 días)
arian hoseini
arian hoseini el 24 de Jun. de 2022
Comentada: arian hoseini el 24 de Jun. de 2022
v=zeros(1,4);
X1=zeros(1,6);
X2=zeros(1,6);
for k=1:6
for j=1:n
X1(1,k)=(j^k)+X1(1,k);
X2(1,k)=(j^k)+X2(1,k);
end
end
g1=zeros(1,4);
g2=zeros(1,4);
for i=1:40
for j=1:4
g1(1,j)=sum((i.^(j-1)).*V(i,1));
g2(1,j)=sum((i.^(j-1)).*I(i,1));
end
end
x1=[40 X1(1,1) X1(1,2) X1(1,3); X1(1,1) X1(1,2) X1(1,3) X1(1,4); X1(1,2) X1(1,3) X1(1,4) X1(1,5); X1(1,3) X1(1,4) X1(1,5) X1(1,6)];
x2=[40 X2(1,1) X2(1,2) X2(1,3); X2(1,1) X2(1,2) X2(1,3) X2(1,4); X2(1,2) X2(1,3) X2(1,4) X2(1,5); X2(1,3) X2(1,4) X2(1,5) X2(1,6)];
k1=inv(x1);
k2=inv(x2);
a1=g1*k1;
a2=g2*k2;
V3=sqrt(((a1(1,2)^2)+(4*(a1(1,3)^2))));
i3=sqrt(((a2(1,2)^2)+(4*(a2(1,3)^2))));
tettav=atan((-2*a1(1,3))/a1(1,2));
tv=rad2deg(tettav);
tettai=atan((-2*a2(1,3))/a2(1,2));
ti=rad2deg(tettai);
Z=V3/i3;
tz=tv-ti;
how can write x1 and x2 with for loop instead of matrix?is it possible?
or is there anway so i could make this code shorter?
and is there any code soi could estimate matlab Measurement time?
  2 comentarios
Walter Roberson
Walter Roberson el 24 de Jun. de 2022
https://www.mathworks.com/help/matlab/ref/toeplitz.html perhaps
Walter Roberson
Walter Roberson el 24 de Jun. de 2022
You can use tic() tock() to estimate execution time

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 24 de Jun. de 2022
Editada: Jan el 24 de Jun. de 2022
X1 = zeros(1,6);
X2 = zeros(1,6);
for k=1:6
for j=1:n
X1(1,k)=(j^k)+X1(1,k);
X2(1,k)=(j^k)+X2(1,k);
end
end
x1 = [40 X1(1,1) X1(1,2) X1(1,3); ...
X1(1,1) X1(1,2) X1(1,3) X1(1,4); ...
X1(1,2) X1(1,3) X1(1,4) X1(1,5); ...
X1(1,3) X1(1,4) X1(1,5) X1(1,6)];
x2 = [40 X2(1,1) X2(1,2) X2(1,3);
X2(1,1) X2(1,2) X2(1,3) X2(1,4); ...
X2(1,2) X2(1,3) X2(1,4) X2(1,5); ...
X2(1,3) X2(1,4) X2(1,5) X2(1,6)];
% Without loops:
X1 = sum((1:6).' .^ (1:6), 1);
X2 = X1;
% More compact:
x1 = [40, X1(1:3); X1(1:4); X1(2:5); X1(3:6)];
x2 = [40, X2(1:3); X2(1:4); X2(2:5); X2(3:6)];
But what is the idea of creating x1 and x2 with loops? Simpler code is easier to debug.
  3 comentarios
Jan
Jan el 24 de Jun. de 2022
Maybe. This might depend on what "n" is in for j=1:n . I guessed boldly that n=6. Now with flexibel n:
X1 = zeros(1,6);
X2 = zeros(1,6);
n = 8;
for k = 1:6
for j = 1:n
X1(1,k)=(j^k)+X1(1,k);
X2(1,k)=(j^k)+X2(1,k);
end
end
X1_ = sum((1:n).' .^ (1:6), 1);
X2_ = X1;
isequal(X1, X1_)
ans = logical
1
isequal(X2, X2_)
ans = logical
1
arian hoseini
arian hoseini el 24 de Jun. de 2022
thank u sir

Iniciar sesión para comentar.

Más respuestas (1)

Voss
Voss el 24 de Jun. de 2022
Editada: Voss el 24 de Jun. de 2022
n = 2;
X1=zeros(1,6);
for k=1:6
for j=1:n
X1(1,k)=(j^k)+X1(1,k);
end
end
X1
X1 = 1×6
3 5 9 17 33 65
% the way you have it now:
x1=[40 X1(1,1) X1(1,2) X1(1,3); X1(1,1) X1(1,2) X1(1,3) X1(1,4); X1(1,2) X1(1,3) X1(1,4) X1(1,5); X1(1,3) X1(1,4) X1(1,5) X1(1,6)]
x1 = 4×4
40 3 5 9 3 5 9 17 5 9 17 33 9 17 33 65
% the same, but written across multiple lines of code:
x1 = [ ...
40 X1(1,1) X1(1,2) X1(1,3); ...
X1(1,1) X1(1,2) X1(1,3) X1(1,4); ...
X1(1,2) X1(1,3) X1(1,4) X1(1,5); ...
X1(1,3) X1(1,4) X1(1,5) X1(1,6)]
x1 = 4×4
40 3 5 9 3 5 9 17 5 9 17 33 9 17 33 65
% another way to do the same - indexing
% sets of elements at once instead of
% each element of X1 individually:
x1 = [ ...
40 X1(1,1:3); ...
X1(1,1:4); ...
X1(1,2:5); ...
X1(1,3:6)]
x1 = 4×4
40 3 5 9 3 5 9 17 5 9 17 33 9 17 33 65
% another way to do the same - since X1 is
% a row vector, you can omit the first (row)
% index, i.e., X1(1,ii) is X1(ii):
x1 = [ ...
40 X1(1:3); ...
X1(1:4); ...
X1(2:5); ...
X1(3:6)]
x1 = 4×4
40 3 5 9 3 5 9 17 5 9 17 33 9 17 33 65
% another way to do the same - do all
% of the indexing into X1 at once:
x1 = 40*ones(4);
idx = (0:3)+(0:3).';
good_idx = idx > 0;
x1(good_idx) = X1(idx(good_idx))
x1 = 4×4
40 3 5 9 3 5 9 17 5 9 17 33 9 17 33 65
  2 comentarios
arian hoseini
arian hoseini el 24 de Jun. de 2022
best answer ever
Voss
Voss el 24 de Jun. de 2022
Thanks!

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos


Versión

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by