get rid of NaN elements in Simulink
조회 수: 40 (최근 30일)
이전 댓글 표시
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
2012년 9월 30일
편집: Azzi Abdelmalek
2012년 9월 30일
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)
댓글 수: 0
Image Analyst
2012년 9월 30일
편집: Image Analyst
2012년 9월 30일
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().
댓글 수: 0
Owen
2012년 10월 2일
댓글 수: 3
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.
Owen
2012년 10월 4일
댓글 수: 8
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
MUHAMMAD ADNAN
2015년 4월 3일
편집: MUHAMMAD ADNAN
2015년 4월 3일
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
2015년 4월 3일
I suggest you start your own new question rather than posting your question as an "Answer" to this one.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!