Hi. I need to analyze the rows of the vector 'value_GS_test' and transform only the numbers 254 and 255 (if any) into '[]' while keeping the same number of rows as in the original vector. Here is an example:
Right now I am using this code, but it only deletes one row (at 255) for me. How can I get the desired result?
value_GS = importdata("value_GS_test.mat"); % 6 elements
for rows_value_GS = 1 %:height(value_GS)
analysis_number = value_GS(rows_value_GS);
if analysis_number == 255
value_GS(rows_value_GS) = [];
elseif analysis_number == 254
value_GS(rows_value_GS) = [];
else
value_GS(rows_value_GS) = value_GS(rows_value_GS);
end
end

댓글 수: 5

Chunru
Chunru 2023년 7월 18일
How about using nan instead of []?
Stephen23
Stephen23 2023년 7월 18일
"How can I get the desired result?"
Numeric arrays cannot have empty elements or "holes" in them: every element is one value.
You could use a marker value, e.g. NaN, or store a separate logical mask.
Walter Roberson
Walter Roberson 2023년 7월 18일
Note however that uint8 cannot represent NaN or inf
Simon Chan
Simon Chan 2023년 7월 18일
What is the final purpose of making a bracket to those positions? For display only? Or you will write it as blank on a text file?
Alberto Acri
Alberto Acri 2023년 7월 18일
For display

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

 채택된 답변

Chunru
Chunru 2023년 7월 18일

0 개 추천

load(websave("value_GS_test.mat", "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1436593/value_GS_test.mat"));
value_GS = double(value_GS)
value_GS = 6×1
255 255 255 68 70 72
value_GS(value_GS>=254) = nan;
value_GS
value_GS = 6×1
NaN NaN NaN 68 70 72

댓글 수: 7

Alberto Acri
Alberto Acri 2023년 7월 18일
Thank you for your reply. I prefer [] instead of '0' or 'NaN'.
Chunru
Chunru 2023년 7월 18일
If you prefer '[]', you may have to use string, which may not be efficient.
Stephen23
Stephen23 2023년 7월 18일
A string will most likely be complex and inefficient. Perhaps a mask would work... but it depends entirely on what they are doing with this data. So first the OP needs to answer this very important question:
Alberto Acri
Alberto Acri 2023년 7월 18일
Thank you for the clarification @Stephen23
Alberto Acri
Alberto Acri 2023년 7월 18일
Hi @Chunru. Is it normal that with your code I am displaying '0's instead of 'NaN'?
Yes, it is.
Refer to @Walter Roberson's comment above - "Note however that uint8 cannot represent NaN or inf".
When you try to assign NaN (or Inf for that matter) to a uint8 (or any integer data type) array, it will display 0 instead of NaN
%Signed integer data type example -
y = int16(1:4)
y = 1×4
1 2 3 4
%Assinging 2nd element of the vector to be NaN
y(2) = NaN
y = 1×4
1 0 3 4
%Unsigned integer data type example -
%Initializing NaN with an unsigned integer leads to 0 as well
z = uint8([1 23 NaN 19 5])
z = 1×5
1 23 0 19 5
Alberto Acri
Alberto Acri 2023년 7월 18일
Okay. Thanks for the explanation @Dyuman Joshi. I am unfamiliar with Matlab.

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2021b

질문:

2023년 7월 18일

댓글:

2023년 7월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by