get rid of NaN elements in Simulink
Mostra commenti meno recenti
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
1 Commento
Azzi Abdelmalek
il 30 Set 2012
what is the loength of your output?
Risposte (6)
Azzi Abdelmalek
il 30 Set 2012
Modificato: Azzi Abdelmalek
il 30 Set 2012
0 voti
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
il 30 Set 2012
Modificato: Image Analyst
il 30 Set 2012
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
il 2 Ott 2012
0 voti
3 Commenti
Azzi Abdelmalek
il 2 Ott 2012
No, it's not about sin Fcn or cos Fcn, Matlab Fcn is different, you can code whatever you want
Mike Hosea
il 2 Ott 2012
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
il 2 Ott 2012
Modificato: Azzi Abdelmalek
il 2 Ott 2012

Owen
il 4 Ott 2012
0 voti
8 Commenti
Azzi Abdelmalek
il 4 Ott 2012
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
il 5 Ott 2012
Azzi Abdelmalek
il 5 Ott 2012
Ok, then the size of the input is 10. what about the size of the output. Is it fixed? It appears no
Owen
il 6 Ott 2012
Azzi Abdelmalek
il 6 Ott 2012
Modificato: Azzi Abdelmalek
il 6 Ott 2012
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
il 7 Ott 2012
Azzi Abdelmalek
il 7 Ott 2012
Modificato: Azzi Abdelmalek
il 7 Ott 2012
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
il 8 Ott 2012
Owen, did you think about my suggestion?
Owen
il 8 Ott 2012
MUHAMMAD ADNAN
il 3 Apr 2015
Modificato: MUHAMMAD ADNAN
il 3 Apr 2015
0 voti
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 Commento
Image Analyst
il 3 Apr 2015
I suggest you start your own new question rather than posting your question as an "Answer" to this one.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!