I am struggling with letter b and keep getting this as an error. Variable b must be of size [1 1]. It is currently of size [3 4]. Check where the variable is assigned a value.

14 次查看(过去 30 天)
Consider the array (row vector), matrix, and column vector below.
A = [11.3 25.4 -58.6 66.2 82.8 37.9 41.5 3.6 60.7 14.2]
B = [1.256 3.135 5.489 7.236; 8.457 2.236 -4.456 6.128; 0.236 7.458 8.569 9.745]
C = [2.23; 3.58; -5.69; 7.47; 8.23; 5.46; 0.19; -9.73; 1.28; 4.46]
a) Sort A in ascending order and then calculate log base 10 of each element.
b) Calculate log base 10 of each element in B and then find the maximum value for the entire matrix.
c) Round all values down and then calculate log base 10 of each element in C.
here is my code:
A = [11.3 25.4 -58.6 66.2 82.8 37.9 41.5 3.6 60.7 14.2]
B = [1.256 3.135 5.489 7.236; 8.457 2.236 -4.456 6.128; 0.236 7.458 8.569 9.745]
C = [2.23; 3.58; -5.69; 7.47; 8.23; 5.46; 1.19; -9.73; 1.28; 4.46]
% a) Sort A in ascending order and then calculate log base 10 of each element.
A = sort(A)
a = log10(A)
% b) Calculate log base 10 of each element in B and then find the maximum value.
b = log10(B)
B = max(B,[],'all')
% c) Round all values down and then calculate log base 10 of each element in C.
C = floor(C)
c = log10(C)

采纳的回答

Jasvin
Jasvin 2023-2-9
Hi Monique,
You just have to reassign the output to b instead of B as you are doing in this case.
Also, here’s another way you can accomplish the same thing in one line,
b = max(max(log10(B)));

更多回答(3 个)

Dyuman Joshi
Dyuman Joshi 2023-2-9
Variable b has been used to store the log10 values of B matrix and it has not been updated afterwards, You have used B instead b
%In this line of code
B = max(B,[],'all')
There are some negative elements in B (and A and C as well) and by definition the input to log should be a positive value. I would suggest you to clarify from your instructor what is to be done, but the function log() accepts negative values as well
B = [1.256 3.135 5.489 7.236; 8.457 2.236 -4.456 6.128; 0.236 7.458 8.569 9.745];
logB = log10(B);
b = max(logB,[],'all')
b = 0.6489 + 1.3644i

Sulaymon Eshkabilov
One typo B instead of b in computing max of log10(B):
A = [11.3 25.4 -58.6 66.2 82.8 37.9 41.5 3.6 60.7 14.2];
B = [1.256 3.135 5.489 7.236; 8.457 2.236 -4.456 6.128; 0.236 7.458 8.569 9.745];
C = [2.23; 3.58; -5.69; 7.47; 8.23; 5.46; 0.19; -9.73; 1.28; 4.46];
% a) Sort A in ascending order and then calculate log base 10 of each element.
A = sort(A)
A = 1×10
-58.6000 3.6000 11.3000 14.2000 25.4000 37.9000 41.5000 60.7000 66.2000 82.8000
a = log10(A)
a =
1.7679 + 1.3644i 0.5563 + 0.0000i 1.0531 + 0.0000i 1.1523 + 0.0000i 1.4048 + 0.0000i 1.5786 + 0.0000i 1.6180 + 0.0000i 1.7832 + 0.0000i 1.8209 + 0.0000i 1.9180 + 0.0000i
% b) Calculate log base 10 of each element in B and then find the maximum value.
b = log10(B)
b =
0.0990 + 0.0000i 0.4962 + 0.0000i 0.7395 + 0.0000i 0.8595 + 0.0000i 0.9272 + 0.0000i 0.3495 + 0.0000i 0.6489 + 1.3644i 0.7873 + 0.0000i -0.6271 + 0.0000i 0.8726 + 0.0000i 0.9329 + 0.0000i 0.9888 + 0.0000i
B = max(b,[],'all')
B = 0.6489 + 1.3644i
% c) Round all values down and then calculate log base 10 of each element in C.
C = floor(C)
C = 10×1
2 3 -6 7 8 5 0 -10 1 4
c = log10(C)
c =
0.3010 + 0.0000i 0.4771 + 0.0000i 0.7782 + 1.3644i 0.8451 + 0.0000i 0.9031 + 0.0000i 0.6990 + 0.0000i -Inf + 0.0000i 1.0000 + 1.3644i 0.0000 + 0.0000i 0.6021 + 0.0000i

Sarthak
Sarthak 2023-2-9
According to the question (b) part, you need to find log10 of all the elements in the matrix. Since the size of B is [3 4], when you apply log10(B) and save it in variable b, the size of b will eventually be [3 4] only. The maximum element out of b will have a size of [1 1] or we can say it will be a scalar.

类别

Help CenterFile Exchange 中查找有关 Matrix Decomposition 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by