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.

조회 수: 1 (최근 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
VBBV
VBBV 2024년 2월 20일
Do you mean multplying with 5* ? or product of 5 consecutive values
Spaceman
Spaceman 2024년 2월 20일
Variable H must be of size [2 2]. Otherwise this looks good. How do we get to the proper size for H?

댓글을 달려면 로그인하십시오.

채택된 답변

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
John D'Errico
John D'Errico 2024년 2월 20일
You were already there. Just getting confused by the wording, as you said, overthinking it.

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by