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

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by