problem with the sum function

1 Ansicht (letzte 30 Tage)
Rodrigo Franco
Rodrigo Franco am 2 Jul. 2020
Beantwortet: Image Analyst am 2 Jul. 2020
Hello everyone, im trying to solve this exercise
but appear a error that i dont know how to solve
"Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead."
this is my code:
function [norma2] = normalizacao(X)
[n, m] = size(X);
for i = 1:n
for j = 1:m
norma2(i, j) = sqrt(sum(X(i, :)^2))
end
end
end
Someone can help me?

Akzeptierte Antwort

madhan ravi
madhan ravi am 2 Jul. 2020
norma2 = sqrt(sum(X.^2))

Weitere Antworten (1)

Image Analyst
Image Analyst am 2 Jul. 2020
You need to leave i as a variable because it did not say to sum over i. So just sum over the j dimension for a given i.
Try this:
norma2 = sqrt(sum(X(i, 1 : M) .^ 2));
No loops needed. Perhaps M is the number of columns, but it didn't say that explicitly so I'm just leaving it as M

Kategorien

Mehr zu Install Products finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by