How to use each value of a matrix in certain formula and also save its result

1 回表示 (過去 30 日間)
Safi ullah
Safi ullah 2017 年 3 月 1 日
回答済み: KSSV 2017 年 3 月 1 日
Hi,I have a matrix having size Y=54×20.I want to use each element (value) of “Y” into the formula Q=(2-squrt(valueofY))./ squrt(valueofY) and save its result.I've tried the following,
P=zeros(54,20);
for j=1:1080;
Q=(2-sqrt(j))./sqrt(j);
P(j,:)=Q;
end

採用された回答

KSSV
KSSV 2017 年 3 月 1 日
Y=rand(54,20);
%%vectorized
A = (2-(Y.^0.5))./(Y.^0.5) ;
%%using loop
[m,n] = size(Y) ;
B = zeros(m,n) ;
for i = 1:m
for j = 1:n
B(i,j) = (2-sqrt(Y(i,j)))/sqrt(Y(i,j));
end
end
You need not to use loop in matlab. Straight away you can use matrices.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by