Stuck on question 2.
Afficher commentaires plus anciens
Question 1.
Write a function, called UpdateVector, that generates a new vector B1 based on a numerical vector A1 of any length, using the following rules: If values in A1 are ≥ 1, calculate the natural logarithm of that element. For values in A1 that are < 1, add 10 to each element.
To follow the rules, you may use a for loop with conditional statements. To get the information on the length of a vector, you can use length command. Your function needs to have one input variable, A1, and one output variable, B1.
Test your function using a vector [4, 5, -1] to make sure the function works.
Question 2.
Write a function, called UpdateArray, that generates a new array B2 based on a numerical array A2 of any size (a number of rows and a number of columns), using the same rules as used in the Question 1 above.
To follow the rules, you may use nested for loops with conditional statements. To get the information on the size of an array, you can use size command. Your function needs to have one input variable, A2, and two output variables (the first one B2, the second one A2).
Check your function using array [4, 5, -1; -7, 10, 9;6, -3, -2] to make sure the function works.
I have completed the first Question, and got the right answers for the second, but wondering if there is a better way to right it. Thanks.
function [B2,A2] = UpdateArray(A2)
%UNTITLED6 Summary of this function goes here
% Detailed explanation goes here
B2 = A2
for m = 1:size(A2)
for d = 1:size(A2)
if A2(m,d)>=1
B2(m,d) = log(A2(m,d));
elseif A2(m,d) < 1
B2(m,d) = A2(m,d) + 10;
end
end
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!