Help with problem?

조회 수: 9 (최근 30일)
Anne Nguyen
Anne Nguyen 2019년 10월 16일
댓글: TA 2019년 10월 16일
What would the value of d be after the following code is executed?
>> vec = linspace(99,44,56);
>> vec = reshape(vec,8,7);
>> d = vec(length(size(vec(:,3))))
I know that the answer is 98, but I do not understand why. A thorough explanation of this would be appreciated. Thank you.

채택된 답변

TA
TA 2019년 10월 16일
After the second step, you get:
vec =
99 91 83 75 67 59 51
98 90 82 74 66 58 50
97 89 81 73 65 57 49
96 88 80 72 64 56 48
95 87 79 71 63 55 47
94 86 78 70 62 54 46
93 85 77 69 61 53 45
92 84 76 68 60 52 44
Third step: (length(size(vec(:,3)))) equals to 2 if you keep dissecting it. Basically, all columns i.e. vec(:,3) have a size of 8 x 1. And length of that "size" is 2.
And thus, you arrive at vec(2). Which is 98 as I indicated above.
  댓글 수: 2
Anne Nguyen
Anne Nguyen 2019년 10월 16일
So just to clarify, vec(:,3) means to include all elements in the third column? And then from there, how is the length of that size = 2? I'm sorry, I'm just still a bit confused. Thank you very much.
TA
TA 2019년 10월 16일
Yes. Anywhere you see a : that implies all elements are considered.
Length gives you the total number of elements in an array. The array size contains two elements i.e. 8 and 1.

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

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by