Unique and vectors last variable (simulink)

16 views (last 30 days)
I got up to 32 vectors with six variables. I would like to remove whole vectors with multiple last variables(6) and keep the rest.
C01 = uint32([1;0;0;0;0;BANK1]);
C02 = uint32([0;1;0;0;0;BANK2]);
C03 = uint32([0;0;1;0;0;BANK3]);
C04 = uint32([0;0;0;1;0;BANK4]);
C05 = uint32([0;0;0;0;1;BANK5]);
Les's say bank 1=5, 2=10, 3=10, 4=20 and 5=0. So the idea is that bank 2 or 3 should be removed, since the value(6) has already been used earlier. But I need to have other vectors safe as whole.
Saved as output:
1 0 0 0 0 5
0 1 0 0 0 10
0 0 0 1 0 20
0 0 0 0 1 0
Removed:
0 0 1 0 0 10
with unique I only know how to check the final variable(6) but everything else of the vector will be lost during process.
Thanks!
  1 Comment
Benjamin Thompson
Benjamin Thompson on 2 Feb 2022
How is this related to Simulink? Can you post an example model showing what you are trying to do?

Sign in to comment.

Accepted Answer

Benjamin Thompson
Benjamin Thompson on 2 Feb 2022
Maybe use this approach in your MATLAB function block or add a separate MATLAB function block to modify the CONNECTION_OPTIONS matrix? Here I call unique twice. Use the 'stable' argument the first time so that column index order in the ic return vector is preserved. Then call unique on that column index vector to remove the repeated values.
>> C = [zeros(5,1) eye(5)]
C =
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
>> CXX = [C; BANK]
CXX =
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 5 10 10 20 0
>> [c, ia, ic] = unique(CXX(end,:), 'stable')
c =
0 5 10 20
ia =
1
2
3
5
ic =
1
2
3
3
4
1
>> CXX(:,unique(ia))
ans =
0 1 0 0
0 0 1 0
0 0 0 0
0 0 0 1
0 0 0 0
0 5 10 20
  2 Comments
Jani Vainionpaa
Jani Vainionpaa on 3 Feb 2022
Ok, thanks for your help!! It seems to be working. With first 6 out of 32 options this is how my end result looks!
function OUTPUT = PFC(C1,C2,C3,C4,C5)
OBJX = [ 0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1 ];
CAPX = [ OBJX; 0 C1 C2 C3 C4 C5 ];
[~, ia, ~] = unique(CAPX(end,:), 'stable');
OUTPUT = uint32(CAPX(:,unique(ia)));
Benjamin Thompson
Benjamin Thompson on 4 Feb 2022
If you received a good answer please mark it accepted!

Sign in to comment.

More Answers (1)

Jani Vainionpaa
Jani Vainionpaa on 2 Feb 2022
Edited: Jani Vainionpaa on 2 Feb 2022
function [CONNECTION_OPTIONS,TEST] = PFC(BANK1,BANK2,BANK3,BANK4,BANK5)
%#codegen
C00 = uint32([0;0;0;0;0;0]);
C01 = uint32([1;0;0;0;0;BANK1]);
C02 = uint32([0;1;0;0;0;BANK2]);
C03 = uint32([0;0;1;0;0;BANK3]);
C04 = uint32([0;0;0;1;0;BANK4]);
C05 = uint32([0;0;0;0;1;BANK5]);
C06 = uint32([1;1;0;0;0;BANK1 + BANK2]);
C07 = uint32([1;0;1;0;0;BANK1 + BANK3]);
C08 = uint32([1;0;0;1;0;BANK1 + BANK4]);
C09 = uint32([1;0;0;0;1;BANK1 + BANK5]);
C10 = uint32([0;1;1;0;0;BANK2 + BANK3]);
C11 = uint32([0;1;0;1;0;BANK2 + BANK4]);
C12 = uint32([0;1;0;0;1;BANK2 + BANK5]);
C13 = uint32([0;0;1;1;0;BANK3 + BANK4]);
C14 = uint32([0;0;1;0;1;BANK3 + BANK5]);
C15 = uint32([0;0;0;1;1;BANK4 + BANK5]);
C16 = uint32([1;1;1;0;0;BANK1 + BANK2 + BANK3]);
C17 = uint32([1;1;0;1;0;BANK1 + BANK2 + BANK4]);
C18 = uint32([1;1;0;0;1;BANK1 + BANK2 + BANK5]);
C19 = uint32([1;0;1;1;0;BANK1 + BANK3 + BANK4]);
C20 = uint32([1;0;1;0;1;BANK1 + BANK3 + BANK5]);
C21 = uint32([1;0;0;1;1;BANK1 + BANK4 + BANK5]);
C22 = uint32([0;1;1;1;0;BANK2 + BANK3 + BANK4]);
C23 = uint32([0;1;1;0;1;BANK2 + BANK3 + BANK5]);
C24 = uint32([0;1;0;1;1;BANK2 + BANK4 + BANK5]);
C25 = uint32([0;0;1;1;1;BANK3 + BANK4 + BANK5]);
C26 = uint32([1;1;1;1;0;BANK1 + BANK2 + BANK3 + BANK4]);
C27 = uint32([1;1;1;0;1;BANK1 + BANK2 + BANK3 + BANK5]);
C28 = uint32([1;1;0;1;1;BANK1 + BANK2 + BANK4 + BANK5]);
C29 = uint32([1;0;1;1;1;BANK1 + BANK3 + BANK4 + BANK5]);
C30 = uint32([0;1;1;1;1;BANK2 + BANK3 + BANK4 + BANK5]);
C31 = uint32([1;1;1;1;1;BANK1 + BANK2 + BANK3 + BANK4 + BANK5]);
CONNECTION_OPTIONS = uint32([C00,C01,C02,C03,C04,C05,C06,C07,C08,C09,C10,C11,C12,C13,C14,C15,C16,C17,C18,C19,C20,C21,C22,C23,C24,C25,C26,C27,C28,C29,C30,C31]);
TEST = unique(uint32([C00(6),C01(6),C02(6)]));

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!