How do I create a variable H which contains the product of 5 and the values of B in the 1st and 9th rows, and the 1st and 2nd column.

2 次查看(过去 30 天)
Given: B=rand(randi([10 20]),randi([5 30]))
Find: Create a variable H which contains the product of 5 and the values of B in the 1st and 9th rows, and the 1st and 2nd column. Variable H must be of size [2 2].
Issue: I am unfamiliar with the wording, does it want 5 * the values of B in ([1,9],[1,2])?
My Solution: I got as far as to state >> H=prod(B([1,9],[1,2])); but I do not know what to do with "the product of 5" piece.
  3 个评论

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2024-2-20
编辑:John D'Errico 2024-2-20
Ok, first, you do understand how to extract the elements from B. Good there. I think maybe the question wording is confusing you.
B=rand(randi([10 20]),randi([5 30]))
B = 15×6
0.5745 0.6058 0.5994 0.6935 0.0956 0.2848 0.8822 0.8884 0.9519 0.3293 0.0266 0.6770 0.4545 0.1755 0.2823 0.9036 0.6515 0.7482 0.7823 0.5347 0.4965 0.6685 0.5269 0.4783 0.8030 0.9700 0.4688 0.5584 0.8849 0.9196 0.7843 0.5541 0.7347 0.1885 0.2401 0.6638 0.4241 0.2129 0.2587 0.7451 0.0305 0.8609 0.9187 0.1319 0.1306 0.4830 0.0690 0.1599 0.5090 0.7634 0.5069 0.6934 0.1676 0.5085 0.2144 0.5469 0.5273 0.1173 0.4948 0.9835
B([1,9],[1,2])
ans = 2×2
0.5745 0.6058 0.5090 0.7634
So the result is a 2x2 array.
The question is not asking you to form the product of all those elements. So there is no reason to use the function prod.
The clue is the result itself must be a 2x2 array. So now all you need to do is multiply all elements of that 2x2 array by the number 5. So you are correct that
5*B([1,9],[1,2])
ans = 2×2
2.8726 3.0291 2.5449 3.8168
is what appears to have been requested.
  3 个评论

请先登录,再进行评论。

更多回答(1 个)

VBBV
VBBV 2024-2-20
Do you mean like this ?
B=rand(randi([10 20]),randi([5 30]));
H = 5*kron(B([1,9]),B([1,2]).')
H = 2×2
2.0647 1.3872 2.0548 1.3806
  1 个评论
Spaceman
Spaceman 2024-2-20
编辑:Spaceman 2024-2-20
I don't think we have learned about kron, (Kronecker tensor product), however I do appreciate your efforts and it did return H with size [2 2].

请先登录,再进行评论。

类别

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

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by