# Match a vector with specified length

**URL:** <https://community.wolfram.com/t/match-a-vector-with-specified-length/20171>\
**Category:** Questions\
**Created:** [June 17, 2022, 12:27pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171 "2022-06-17T12:27:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [June 17, 2022, 12:27pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/1 "2022-06-17T12:27:42Z")

</div>

I want to define a function parameter which has to be a 3D- or any-dimensional vector.

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/7048de0a-0861-45e6-8778-a575d1436f4c)

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 17, 2022, 3:47pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/2 "2022-06-17T15:47:56Z")

</div>

I don’t know what you’re looking for in terms of “simpler”, but I think it makes the subsequent code simpler if you wrap the conditions up in a well-named function. Parameterizing it also makes it “simpler” to extend later. Here’s one way you could do that.

```
VectorTest[length_, test_][expr_] := 
 And[length == Length@expr, VectorQ[expr, test]]

```

Then your argument could be specified like this:

```
v_?(VectorTest[3, NumericQ])

```

(VectorTest is a pretty generic name, but you can probably come up with something better in your context.)

---

<div class="post-metadata">

**Author:** ![robertnowak](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/robertnowak/32/35992_2.png) [@robertnowak](https://community.wolfram.com/u/robertnowak)\
**Post date:** [June 17, 2022, 4:13pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/3 "2022-06-17T16:13:57Z")

</div>

MatchQ[{1, 20, 300}, {Repeated[\_?AtomQ, 3]}]

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [June 17, 2022, 4:53pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/4 "2022-06-17T16:53:11Z")

</div>

Yes Robert, this tests like VectorQ.  
But extending it to NumericQ would probably not be very readable.

---

<div class="post-metadata">

**Author:** ![robertnowak](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/robertnowak/32/35992_2.png) [@robertnowak](https://community.wolfram.com/u/robertnowak)\
**Post date:** [June 17, 2022, 4:56pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/5 "2022-06-17T16:56:34Z")

</div>

Hi Werner

“would be not very readable” - why that?

```
MatchQ[{1, 20, 300}, {Repeated[_?NumericQ, 3]}]

```

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 17, 2022, 4:57pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/6 "2022-06-17T16:57:27Z")

</div>

Why not just replace AtomQ with NumericQ?

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [June 17, 2022, 6:15pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/7 "2022-06-17T18:15:18Z")

</div>

Robert, this solution with NumericQ works indeed. I had in mind that NumericQ was Listable, but it isn’t.

```
MatchQ[{1, {20, 21}, 300}, {Repeated[_?NumericQ, 3]}]
(* False *)

```

But your original proposal does not really work since AtomQ accepts less expressions than VectorQ:

```
(* This works *)
b =.; vec = {1, b, 300};
{MatchQ[vec, {Repeated[_?AtomQ, 3]}], 
 MatchQ[vec, v : _ /; (VectorQ[v] && Length[v] == 3)]}
(* {True, True} *)

(* This doesn't work *)
b = Sqrt[2]; vec = {1, b, 300};
{MatchQ[vec, {Repeated[_?AtomQ, 3]}], 
 MatchQ[vec, v : _ /; (VectorQ[v] && Length[v] == 3)]}
(* {False, True} *)

(* But at least for NumericQ, it works *)
b = Sqrt[2]; vec = {1, b, 300};
{MatchQ[vec, {Repeated[_?NumericQ, 3]}], 
 MatchQ[vec, v : _ /; (VectorQ[v, NumericQ] && Length[v] == 3)]}
(* {True, True} *)

```

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [June 17, 2022, 6:25pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/8 "2022-06-17T18:25:39Z")

</div>

Thats a very good idea, Eric. Thank you. With some minor changes now I use:

```
ClearAll[myVectorLQ];
myVectorLQ[length_][expr_] := TrueQ[length == Length@expr] && VectorQ[expr]; 
myVectorLQ[length_, test_][expr_] := 
 TrueQ[length == Length@expr] && VectorQ[expr, test];

```

Then I can write:

```
ClearAll[fL, fLN];
fL[v_?(myVectorLQ[5])] := Style["fL OK", Blue, Bold];
fLN[v_?(myVectorLQ[5, NumericQ])] := Style["fLN OK", Blue, Bold];

a = Sqrt[2]; b =.;
Do[
  Print["v = ", Style[v, Bold], ", {fL[v],fLN[v]} = ", {fL[v], fLN[v]}];
  , {v, {{1, a, 3, 4, 5}, {1, a, b, 4, 5}, {1, {2 a}, 3, 4, 5}, {1, a, 3, 
     4}}}];

```

 ![10210b1.jpg](https://global.discourse-cdn.com/wolfram/original/3X/c/2/c28be743d98aa64d0fdc0121482aba69a81c0c58.jpeg)

This also solves the problem of passing a default value:

```
ClearAll[fLD, fLND];
fLD[v : (_?(myVectorLQ[5])) : {1, 2, 3, 4, 5}] := Style["fLD OK", Blue, Bold];
fLND[v : (_?(myVectorLQ[5, NumericQ])) : {1, 2, 3, 4, 5}] := 
  Style["fLND OK", Blue, Bold];

a = Sqrt[2]; b =.;
Do[
  Print["v = ", Style[If[v != {}, First[v], v], Bold], 
    ", {fLD[v],fLND[v]} = ", {fLD @@ v, fLND @@ v}];
  , {v, {{{1, a, 3, 4, 5}}, {{1, a, b, 4, 5}}, {{1, {2 a}, 3, 4, 5}}, {{1, a, 
      3, 4}}, {}}}];

```

 ![6816b2.jpg](https://global.discourse-cdn.com/wolfram/original/3X/9/e/9e43e04a203d57d0ae0ee3008f9da0f16b6b5a17.jpeg)

---

<div class="post-metadata">

**Author:** ![robertnowak](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/robertnowak/32/35992_2.png) [@robertnowak](https://community.wolfram.com/u/robertnowak)\
**Post date:** [June 17, 2022, 6:42pm UTC](https://community.wolfram.com/t/match-a-vector-with-specified-length/20171/9 "2022-06-17T18:42:25Z")

</div>

Hello Werner

Hope you like this one:

```
In[25]:= MatchQ[{1, 20, 300}, {Repeated[Except[_List], 3]}]    
Out[25]= True

In[24]:= MatchQ[{1, {20}, 300}, {Repeated[Except[_List], 3]}]    
Out[24]= False

```

This follows exactly the Mathematica documentation of VectorQ

VectorQ[expr]  
gives True if expr is a list or a one-dimensional SparseArray object, none of whose elements are themselves lists, and gives False otherwise.

Last but not least:

```
MatchQ[{1, 20, 300}, _?(v \[Function] VectorQ@v && Length@v == 3)]

```
