Simulink vector index assignment / re-ordering using Selector block

I'm trying to merge two vector signals into one vector according to two sets of indices.
For example:
u_pos = [2 3 4];
du_pos = [1 5 6 7];
These indicate the 'indices' of the combined signal where each element of each of the two input vectors is intended to go.
One way to do this might be to use a selector block like this:
This achieves the result, but to get the the correct assignment of the elements as above, I need to provide the following index vector to the selector block:
[4 1 2 3 5 6 7]
These are the indices of the input vector, not the output vector—which I have in my two index vectors above.
Obviously I could use two assignment blocks instead, but it's a more complicated solution than the selector block and also requires a zeros vector of the correct size:
Also, I like that the selector block shows the connections graphically. This is important to me.
Is there an easy way I can use a selector block? Maybe by converting my index vectors perhaps.

1 comentario

Here is one way to convert the 'output index' vector to an 'input index':
>> [~,idx] = ismember(1:length([u_pos du_pos]), [u_pos du_pos])
idx =
4 1 2 3 5 6 7
but I'm not sure I can put this in the Simulink selector dialog cell can I?

Iniciar sesión para comentar.

 Respuesta aceptada

Paul
Paul el 4 de Mzo. de 2022
Normally, you can put any Matlab expression in a block parameter dialog. So if u_pos and du_pos are defined in a workspace that the Selector block can access, you sould be able to put the "ismember( ..." expression in the dialog for the Index parameter.

5 comentarios

Okay, but will it work if idx is the second argument returned by ismember?
No, that won't work. I must have had a brain cramp.
You could put a mask over the Selector block, make u_pos and du_pos the mask dialog parameters, and then compute the Input Port Size and the Index vector (idx) using the ismember function in the mask initialization code.
Thanks. I did this and it works. It would be nice if the selector provided both the option of 'input index vector' (currently provided as 'Index vector') as well as an 'output index vector'. Although that would make it more complex and perhaps more confusing.
I think the Assignment block is really what's expected to be used for this operation.
It can be done with a one Assignment block, with u and du muxed, the output of the mux feeding the Y0 and U input ports of the Assignment block, and its Index parameter being [u_pos du_pos]. So not more complicated than the Selector approach (IMO) and no need to define the constant zero vector. The only thing it doesn't do is graphically show the rearrangement on the block icon.
That's a good suggestion. Didn't think of that. Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2019b

Preguntada:

el 4 de Mzo. de 2022

Comentada:

el 5 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by