Help with problem?

1 次查看(过去 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