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

1 回表示 (過去 30 日間)
arian hoseini
arian hoseini 2022 年 6 月 24 日
コメント済み: arian hoseini 2022 年 6 月 24 日
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 6 月 24 日
https://www.mathworks.com/help/matlab/ref/toeplitz.html perhaps
Walter Roberson
Walter Roberson 2022 年 6 月 24 日
You can use tic() tock() to estimate execution time

サインインしてコメントする。

採用された回答

Jan
Jan 2022 年 6 月 24 日
編集済み: Jan 2022 年 6 月 24 日
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 件のコメント
Jan
Jan 2022 年 6 月 24 日
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 2022 年 6 月 24 日
thank u sir

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2022 年 6 月 24 日
編集済み: Voss 2022 年 6 月 24 日
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 件のコメント
arian hoseini
arian hoseini 2022 年 6 月 24 日
best answer ever
Voss
Voss 2022 年 6 月 24 日
Thanks!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by