What's wrong with this code?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Leandro Cavalheiro
am 26 Jun. 2016
Bearbeitet: the cyclist
am 26 Jun. 2016
Dear fellows. I was asked to create a function for which the input is a matrix M(n,m) and the output is also a matrix, Q(n,m), so that Q(n,m) is sin (M(n,m)) if M(n,m) is even and Q(n,m) is cos (M(n,m)) if M(n,m) is odd. That's what I did:
function [Q] = myTrigOddEven(M)
n = size(M,1);
m = size(M,2);
for i = 1:n
for j = 1:m
if rem(M(n,m),2)== 0
Q(n,m) = sin(M(n,m))
else
Q(n,m) = cos (M(n,m));
end
end
end
I tested it with the matrix A = [1 2; 3 4] but I got [0 0 ; 0 -0.7568] as a result. What did I do wrong?
0 Kommentare
Akzeptierte Antwort
the cyclist
am 26 Jun. 2016
Bearbeitet: the cyclist
am 26 Jun. 2016
In these lines, you should be indexing with (i,j) rather than (n,m)
if rem(M(n,m),2)== 0
Q(n,m) = sin(M(n,m))
else
Q(n,m) = cos (M(n,m));
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Analog Devices ADALM1000 Support from Data Acquisition Toolbox 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!