i want to take different values in each loop ?

1 回表示 (過去 30 日間)
kitty varghese
kitty varghese 2017 年 8 月 10 日
編集済み: Jan 2017 年 8 月 10 日
close all;
clear all;
clc;
v=[1 2 3;4 5 6;7 8 9]
[n m]=size(v);
w=double(abs(rand(n,5)));
h=double(abs(rand(5,m)));
% L2=norm(w,2);
l2=zeros(3,1)
%l2 norm column wise
for i=1:5
l2(i)=norm(w(:,i),2)
end
%l1 norm column wise
l1=zeros(5,1);
for k=1:5
l1(k)=(l2(k)*(sqrt(3)-(sqrt(3)-1)*0.9))
end
%%i want to make a loop such that it takes value of l1 for each w
%%that is I want to avoid rewriting the below code for taking each value of l1.
for i1=1:3
w(i1)=w(i1)+(l1(1)-sum(w(:,i1)))/3
end
for i2=1:3
w(i2)=w(i2)+(l1(2)-sum(w(:,i2)))/3
end
for i3=1:3
w(i3)=w(i3)+(l1(3)-sum(w(:,i3)))/3
end
for i4=1:3
w(i4)=w(i4)+(l1(4)-sum(w(:,i4)))/3
end
for i5=1:3
w(i5)=w(i5)+(l1(5)-sum(w(:,i5)))/3
end
  1 件のコメント
Stephen23
Stephen23 2017 年 8 月 10 日
@ kitty varghese: why are you using loops anyway? These are simple arithmetic operations, which could easily be vectorized:

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

採用された回答

kitty varghese
kitty varghese 2017 年 8 月 10 日
編集済み: Stephen23 2017 年 8 月 10 日
u can make an outer loop for changing the value of l1
for i0=1:5
for i1=1:3
w(i1)=w(i1)+(l1(i0)-sum(w(:,i1)))/3
end
end
  1 件のコメント
Jan
Jan 2017 年 8 月 10 日
編集済み: Jan 2017 年 8 月 10 日
This looks strange:
w(i1)=w(i1)+(l1(1)-sum(w(:,i1)))/3
you access w as a vector on the left, and as a matrix on the right.
w=double(abs(rand(n,5)));
is the same as
w = rand(n,5);
Instead of
l1=zeros(5,1);
for k=1:5
l1(k)=(l2(k)*(sqrt(3)-(sqrt(3)-1)*0.9))
end
you can write:
l1 = l2 * (sqrt(3)-(sqrt(3)-1)*0.9));
This is nicer and faster.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by