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

1 view (last 30 days)
arian hoseini on 24 Jun 2022
Commented: arian hoseini on 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));
tettai=atan((-2*a2(1,3))/a2(1,2));
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?
Walter Roberson on 24 Jun 2022
You can use tic() tock() to estimate execution time

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.
arian hoseini on 24 Jun 2022
thank u sir

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
Voss on 24 Jun 2022
Thanks!

R2016b

Community Treasure Hunt

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

Start Hunting!