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

2 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 24 Jun 2022
https://www.mathworks.com/help/matlab/ref/toeplitz.html perhaps
Walter Roberson
Walter Roberson on 24 Jun 2022
You can use tic() tock() to estimate execution time

Sign in to comment.

Accepted Answer

Jan
Jan on 24 Jun 2022
Edited: Jan on 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 Comments
Jan
Jan on 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

Sign in to comment.

More Answers (1)

Voss
Voss on 24 Jun 2022
Edited: Voss on 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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!