Stuck on question 2.

5 views (last 30 days)
James Harrison
James Harrison on 19 Oct 2020
Commented: James Harrison on 19 Oct 2020
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

Accepted Answer

Steven Lord
Steven Lord on 19 Oct 2020
Edited: Steven Lord on 19 Oct 2020
Under the constraints that you have to use nested for loops and that this only has to work for matrices, not N-dimensional arrays with N > 2, I only see one bug (in two places) in your code.
The size function, when called with one input and one output, will return a vector with one element per dimension in the array you passed into it.
A = ones(2, 3);
szA = size(A) % szA is [2 3]
B = zeros(3, 4, 5);
szB = size(B) % szB is [3 4 5]
When you use a non-scalar (like a vector or a matrix) as one of the values in the colon operator : MATLAB will only use the first element of that non-scalar when building the colon vector.
x = [5 6];
y = 1:x % equivalent to 1:5
So in your code, since your test case was a 3-by-3 matrix and size returned [3 3] for that matrix, both your loops were looping over 1:3. Since your test case had the same number of rows and columns this was fine. But if you tried a non-square matrix as a test case, it wouldn't work correctly.
C = [4, 5, -1; -7, 10, 9];
D = [C; C];
If you call size with two input arguments, the output will be a scalar that is the size of the matrix in that dimension.
B = zeros(3, 4, 5);
colsB = size(B, 2) % colsB is 4, the second element of the size vector [3 4 5]
Or you can call size with one output argument per dimension of the array.
B = zeros(3, 4, 5);
[nrows, ncols, npages] = size(B) % nrows is 3, ncols is 4, npages is 5
There's some trickiness if the number of outputs is different than the number of dimensions, so I'd recommend the two input and one output form of size.
As a stylistic suggestion, I recommend using variable names that show the meaning of what's stored in those variables. To me, m and d don't really mean anything and so if this code were longer you might lose track of what they represent. But if you named them whichRow and whichCol, it is a bit more typing (though tab completion can help offset that) but it's much clearer to anyone even glancing at your code what they represent.
  1 Comment
James Harrison
James Harrison on 19 Oct 2020
Okay, this has helped greatly. Could you explain what you mean by tab completion?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!