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

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

KSSV
KSSV il 1 Mar 2017
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.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by