MATLAB Answers

Help with problem?

1 view (last 30 days)
Anne Nguyen
Anne Nguyen on 16 Oct 2019
Commented: TA on 16 Oct 2019
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.

  0 Comments

Sign in to comment.

Accepted Answer

TA
TA on 16 Oct 2019
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 Comments

Anne Nguyen
Anne Nguyen on 16 Oct 2019
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 on 16 Oct 2019
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.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!