The splitapply function applies the function mentioned as an argument to each group. For the ranksum function we would need to provide two sets of data. We can provide these by partitioning the data using findgroups.
You can use the following code for reference and adapt it to your use case:
[grpNums, grps] = findgroups(k);
off = y(k==grps(1));
on = y(k==grps(2));
[p,h] = ranksum(on,off);
Here’s the documentation of findgroups for reference: