i need help with this>?

Hi all
i need help with my assignment
a)using for loop
b)using array masking
Write a function that accepts an array A in its arguments and returns the array B that is computed from array A such that: a. Elements in A less than or equal to -2 are replaced by 30. b. Elements in A greater than -2 and less than 5 are replaced by 15. c. Elements greater than 5 are replaced by 5. Test your function with A=[2,5,10;-8,-1,11;43,6,-2]

1 Kommentar

Rizwana
Rizwana am 24 Mär. 2014
Bearbeitet: Rizwana am 24 Mär. 2014
for ex:
A=[2,5,10;-8,-1,11;43,6,-2];
>> A(A < -2) = 30

Antworten (1)

Chandrasekhar
Chandrasekhar am 24 Mär. 2014
Bearbeitet: Chandrasekhar am 24 Mär. 2014

0 Stimmen

A=[2,5,10;-8,-1,11;43,6,-2];
function B = ReplaceFunction(A)
[m,n] = size(A);
for i = 1:m
for j = 1:n
if(A(i,j)<=-2)
B(i,j) = 30;
else if(A(i,j)>-2 && A(i,j)<=5)
B(i,j) = 15;
else
B(i,j) = 5;
end
end
end
end

3 Kommentare

Rizwana
Rizwana am 24 Mär. 2014
ans = [15 30 5];
How to produce answer with specified data is replaced and original data which falls out of if condition is unchanged...
Chandrasekhar
Chandrasekhar am 24 Mär. 2014
A(A>5) = 5.
elements of A which are greater than 5 are replaced with 5
Is this what you are looking for?
Rizwana
Rizwana am 24 Mär. 2014
Yeah your edited function is working good. Thanks. Just wanted to improve my logic

Diese Frage ist geschlossen.

Gefragt:

am 24 Mär. 2014

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by