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 中查找有关 Programmatic Model Editing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!