cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Referencing parts of vectors and arrays

gshockxcc
7-Bedrock

Referencing parts of vectors and arrays

I'm playing around with selected elements of a vector, or array.  Suppose I only want elements 2..6.  Why does Mathcad fail when using a range for the index, but defining an index as a range does not?  Curious.

gshockxcc_2-1672854481184.png

Thanks in advance.

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:gshockxcc)

Mathcad doesn't fail, but simply wasn't designed to allow an immediate range in the index. So it gives an error message when you try.

You could consider to request this as a (new) feature. Maybe, someday, it might get implemented.

Until then: If you need a subset of a vector or array, use the submatrix() function. Look it up in the help.

 

Success!
Luc

View solution in original post

5 REPLIES 5
RantEng
14-Alexandrite
(To:gshockxcc)

Have you tried using the submatrix function?

 

Bob

LucMeekes
23-Emerald III
(To:gshockxcc)

Mathcad doesn't fail, but simply wasn't designed to allow an immediate range in the index. So it gives an error message when you try.

You could consider to request this as a (new) feature. Maybe, someday, it might get implemented.

Until then: If you need a subset of a vector or array, use the submatrix() function. Look it up in the help.

 

Success!
Luc

HI Luc, 

Thanks for the comment.  As you and RantEng mentioned, I did look up the submatrix function, but was trying to find a quicker way around it.  It's just an intermediate step, I suppose.

 

Thank you.

Werner_E
25-Diamond I
(To:gshockxcc)

Actually, while m[i (which you think is working) looks like a vector, it isn't! Its neither a vector, nor a range. Its an invalid Prime object and you can prove that by trying to assign it a variable - you get an error. So this method may be useful just to display part of the vector.

Werner_E_0-1672907194010.png

So to create a subvector, you have two options.

Either, as was already mentioned, you use the "submatrix" command or, if you really want to avoid this function, you actually can do it by using a range variable but you would have to do it the way shown in the picture below (v2).

Werner_E_1-1672907455839.png

Trying to avoid the definition of the range i on worksheet level by using an inline definition fails as Prime isn't written to accommodate that.

Werner_E_4-1672907743875.png

We can't define a range inline. In a program we would have to use a for-loop to achieve a similar effect

Werner_E_5-1672907860776.png

So my preference would be to use "submatrix" 🙂

If you have to use it more often in your sheet, it might be beneficial to write your own "subvec" function (in a collapsed area on top of the worksheet) to avoid to have to provide the two 0 columns all the time:

Werner_E_6-1672908078032.png

 

 

 

 


@Werner_E wrote:

So my preference would be to use "submatrix" 🙂

I would have to agree!  😂

 

Thank you for the detailed explanation.  I do appreciate it.  Using Submatrix isn't all that much more difficult, as you said.  So, I'll heed your suggestions and follow that method.  

Regards, 

Announcements

Top Tags