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

1 Ansicht (letzte 30 Tage)
arian hoseini
arian hoseini am 24 Jun. 2022
Kommentiert: arian hoseini am 24 Jun. 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 Kommentare
Walter Roberson
Walter Roberson am 24 Jun. 2022
https://www.mathworks.com/help/matlab/ref/toeplitz.html perhaps
Walter Roberson
Walter Roberson am 24 Jun. 2022
You can use tic() tock() to estimate execution time

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 24 Jun. 2022
Bearbeitet: Jan am 24 Jun. 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 Kommentare
Jan
Jan am 24 Jun. 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 am 24 Jun. 2022
thank u sir

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Voss
Voss am 24 Jun. 2022
Bearbeitet: Voss am 24 Jun. 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by