get rid of NaN elements in Simulink

Hi,
I have a special work for Simulink with which Simulink is maybe overloaded.
Many vectors of length 10 shall be processed by a model. The vectors can have all the elements as ‘NaN’ or part of elements as ‘NaN’.
Example:
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 2 3 4 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN
...
This model should get rid of all ‘NaN’ and create a new vector with a fixed length.
Example:
1 2 3 4
5 6 7 8
.
Is it possible with Simulink at all?
As far as I know, I can make it something like that in Matlab
vec(isnan(vec)) = [];
but this method is difficult to integrate in Simulink.
Thanks Senmeis

답변 (6개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 30일
편집: Azzi Abdelmalek 2012년 9월 30일

0 개 추천

if the length of your output is fixed. you can use Embedded Matlab Function or Matlab Fcn block from Simulink/Users-Defined-Functions
Some functions are not allowed by embedded function (now called Matlab Function) while they are allowed by Matlab Fcn (now called Interpreted Matlab Function)
Image Analyst
Image Analyst 2012년 9월 30일
편집: Image Analyst 2012년 9월 30일

0 개 추천

Try this:
m = [NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 2 3 4 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN]
% Find out where the nans are living.
nanMap = isnan(m)
% Find out which rows are all nans in every column.
allNanRows = sum(nanMap, 2) == size(m, 2)
% Get our output
mOut = m; % Make a copy.
mOut(allNanRows, :) = []
In the command window:
m =
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 2 3 4 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN
nanMap =
1 1 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1
allNanRows =
1
0
1
0
mOut =
1 2 3 4 NaN NaN NaN NaN NaN NaN
5 6 7 8 NaN NaN NaN NaN NaN NaN
Note: since there might be variable numbers of Nan, and in different columns, you can't get rid of them in the columns unless there are all the same number of nan's in each row. Add this code:
nanLocations = isnan(mOut)
numberOfGoodColumns = sum(~nanLocations(1,:))
numberOfRows = size(mOut, 1);
mOut(nanLocations) = [] % Erase all nans
mOut = reshape(mOut, [numberOfRows, numberOfGoodColumns])
mOut =
1 2 3 4
5 6 7 8
Though, I'm sure someone will distill that down into one compact and cryptic line with arrayfunc().
Owen
Owen 2012년 10월 2일

0 개 추천

Thank you you both.
I must add that the vectors come one after another, which means, my model shall process only one vector at a time. If all the elements in this vector are 'NaN', then this vector shall be thrown away. If part of elements in this vector is 'NaN', then this part shall be thrown away.
As for Matlab Fcn, I think it requires an expression such as "sin". The command "vec(isnan(vec)) = [];" is not an expression because of '=' so it cannot be integrated into Matlab Fcn.
Thanks Senmeis

댓글 수: 3

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 2일
No, it's not about sin Fcn or cos Fcn, Matlab Fcn is different, you can code whatever you want
Mike Hosea
Mike Hosea 2012년 10월 2일
It's called a "MATLAB Function Block". It was previously known as "Embedded MATLAB Block". Note that if the vectors are processed one at a time, it would be better to process them one at a time in the MATLAB Function Block because it is compiled. That changes the problem quite a bit. If the non-NaN elements are always first, then you can just check if isnan(v(1)) and then do whatever for this "throw it away" case. If the non-NaN values are sprinkled around, you can use all(isnan(v)) or any(~isnan(v)) or count them first and then create a vector and copy them in, etc., etc. I'd need some details to write the code, but the point is that in a MATLAB Function Block, you just write loops to your heart's content because the thing works by generating C code and compiling it.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 2일
편집: Azzi Abdelmalek 2012년 10월 2일

댓글을 달려면 로그인하십시오.

Owen
Owen 2012년 10월 4일

0 개 추천

Thank you.
All non-NaN elements come before NaN elements, but the number of non-NaN elements can be variant. The output length is 10.
I didn’t try Embedded Matlab Block before, but I worry there may be problems with sample time. In Simulink all simulation steps are synchronous with the sample time. What happens to the sample time if all the NaN elements are thrown away? Can the output sample time be adapted automatically?
Thanks Senmeis

댓글 수: 8

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 4일
how your inputs come? what is the length of your input? did they come at one time or step by step? can you show us the part of your diagram involved.
Owen
Owen 2012년 10월 5일
This is part of a receiver which accepts frame based data. As descreibed before, the length of this frame based vector is 10 and the vectors come step by step, that means, at first the first vector with 10 elements, and then the second vector with another 10 elements and so on.
Senmeis
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 5일
Ok, then the size of the input is 10. what about the size of the output. Is it fixed? It appears no
Owen
Owen 2012년 10월 6일
The output length is 4, but it doesn’t play a great roll because all the outputs must be compared with the sent signals.
Senmeis
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 6일
편집: Azzi Abdelmalek 2012년 10월 6일
from your example when the input is
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
what should be the output. each step time whe have to compute the output. and its length should be 4
Owen
Owen 2012년 10월 7일
That’s just the point I’m worrying about. The NaN element means nonsense data, which is the byproduct of the receiver. In principle I don’t need the time information of the output, but as you know, I cannot get rid of the sampling at the output.
Senmeis
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 7일
편집: Azzi Abdelmalek 2012년 10월 7일
What I suggest is to output 4 data + 1 control data
example
input=NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
output_data= NaN NaN NaN NaN, control_data=0
input = 1 2 3 4 NaN NaN NaN NaN NaN NaN
output_data= 1 2 3 4, control_data=1
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 8일
Owen, did you think about my suggestion?

댓글을 달려면 로그인하십시오.

Owen
Owen 2012년 10월 8일

0 개 추천

I tried following code in a Embedded MATLAB Function block.
function y = fcn(n)
i1 = 0;
var = isnan(n);
len = length(var);
for i = 1:len
if var(i) == 0
i1 = i1+1;
end;
end;
% get rid of NaN elements in a vector
if i1 ~= len
y = u(1:i1);
% if all elements are NaN, make it an empty vector
else
y = [];
end;
The problem is, the NaN vectors still exist, but I want to eliminate them. It seems impossible in Simulink.
Thanks Senmeis
MUHAMMAD  ADNAN
MUHAMMAD ADNAN 2015년 4월 3일
편집: MUHAMMAD ADNAN 2015년 4월 3일

0 개 추천

Dear all please help me . I am working with Neural Network toolbox . The problem is that variable valTargets,trainTargets and test Targets has Min and Max value is NaN . I'm importing a Excel data, but once it reads and imports the data, the variable's mins and maxs are NaN. Why is MATLAB giving me this error? How can I solve the NaN value to exact values. All other variable work accurate like input,output,trainperformance ,valperformance.Thanks in Advance. Name value Min Max testTargets 1x4142 double NaN NaN trainTargets 1x4142 double NaN NaN valTargets 1x4142 double NaN NaN Thanks

댓글 수: 1

Image Analyst
Image Analyst 2015년 4월 3일
I suggest you start your own new question rather than posting your question as an "Answer" to this one.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2012년 9월 30일

댓글:

2015년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by