I used fun block in simulink with the following program
function [Y1, Y2,Y3] = selectionSort( Vc1, Vc2, Vc3)
A=[ Vc1, Vc2, Vc3];
B = sort(A) ;
B1=B(1);B2=B(2);B3=B(3);
Y1=0; Y2=0; Y3=0;
K1=find(A==B(1),1, 'first')
K2=find(A==B(2),1, 'first')
K3=find(A==B(3),1, 'first')
n=1
if n==1 && K1==1
Y1=1;Y2=0;Y3=0;Y4=0;Y5=0;Y6=0;Y7=0;Y8=0;Y9=0;Y10=0;
end
I get this error
Expected a scalar. Non-scalars are not supported in IF or WHILE statements, or with logical operators. Instead, use ALL to convert matrix logicals to their scalar equivalents. Function 'MATLAB Function' (#207.737.742), line 19, column 12: "K1==1"
Please I need your help to solve this problem

4 件のコメント

Walter Roberson
Walter Roberson 2019 年 2 月 25 日
Are Vc1, Vc2, Vc3 certain to be scalars? If not then are they row vectors or are they column vectors or 2D ?
Arkanra Kadhum
Arkanra Kadhum 2019 年 2 月 25 日
Hi Roberson
Thank you for your replay answer. These variables are column vector as 3, 7,1
My problem is to read the inputs column then sorted accident and change the output ascending order corroding to their indices. So to send 1 to Y1 , 3 to Y2 and 7 to Y3.
The problem is in using K1, K2 and K3 with if statement, so how can I change them from vector to scalar.
Please replace last line by
Y1=1;Y2=0;Y3=0
Best regards
Arkan
Walter Roberson
Walter Roberson 2019 年 2 月 25 日
Well, change the code to use all(K1==1) like it suggests. Sometimes you have to take some extra steps because Simulink's automatic checking is not as intelligent as humans are.
Arkanra Kadhum
Arkanra Kadhum 2019 年 2 月 25 日
thank Roberson it is work

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2019 年 2 月 25 日

1 投票

Your code is too complex for MATLAB to be able to prove that all of the B values are certain to compare equal to part of A, so MATLAB has to assume that the find() can return empty.
By the way, some of the values can be empty, if there are nan in the inputs.
If the exercise is to implement selection sort, then you are unlikely to pass with that sort() call in your code. Might as well just call sort() with two outputs and use the returned indices.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by