Insert blank cells in a matrix
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
How can I replace all the NaN values of a matrix with a blank space?
채택된 답변
Walter Roberson
2021년 11월 30일
You cannot do that. Numeric matrices cannot have spaces in them.
You can convert the matrix into a cell array and put spaces into the cell array... but it is not clear why you would do that.
myCell = num2cell(MyMatrix);
myCell(isnan(MyMatrix)) = {' '}; %nan -> space
Now what?
댓글 수: 12
Emilio Pulli
2021년 11월 30일
편집: Emilio Pulli
2021년 11월 30일
Because I have a huge matrix which is composed of blocks of numbers (sub-matrices) which are separated by NaN rows. This huge matrix needs to be converted in a txt files where the rows with NaN values must be converted into delimiting blank spaces
Emilio Pulli
2021년 11월 30일
Anyway, your code doesn't work...
MyMatrix = rand(4,4);
MyMatrix(randi(16,1,2)) = nan
MyMatrix = 4×4
0.9622 0.3137 0.0406 0.5866
0.3809 0.8690 NaN 0.3173
0.7191 0.4954 NaN 0.2221
0.1190 0.2682 0.8609 0.7682
myCell = num2cell(MyMatrix);
myCell(isnan(MyMatrix)) = {' '}; %nan -> space
myCell
myCell = 4×4 cell array
{[0.9622]} {[0.3137]} {[0.0406]} {[0.5866]}
{[0.3809]} {[0.8690]} {' ' } {[0.3173]}
{[0.7191]} {[0.4954]} {' ' } {[0.2221]}
{[0.1190]} {[0.2682]} {[0.8609]} {[0.7682]}
Looks to me as if it works.
Walter Roberson
2021년 11월 30일
This huge matrix needs to be converted in a txt files where the rows with NaN values must be converted into delimiting blank spaces
Do I understand correctly that you have rows that are all-NaN and that those must be converted to empty rows ?
How are you doing the writing at present?
How many digits of precision do you need when writing out the data?
Emilio Pulli
2021년 11월 30일
편집: Emilio Pulli
2021년 11월 30일
Yes, exactly you got the point. At the moment, I simply write a txt file using the matrix with the NaN rows. Maybe, I have to convert the matrix with the NaN rows into the txt file and after I need a piece of code that enter the txt file and modifies it by replacing the NaN rows with blank spaces
Walter Roberson
2021년 11월 30일
편집: Walter Roberson
2021년 11월 30일
What commands are you currently using to write the matrix to the file?
At the time that the matrix is to be written to the file, is there still information hanging around about where the NaN rows are? Perhaps they are at regular intervals? Perhaps you happened to already locate the locations in order to write the NaN into there in the first place?
Emilio Pulli
2021년 12월 1일
편집: Emilio Pulli
2021년 12월 1일
The NaN are each 100 rows...therefore I have NaN rows at row number 101, 201, 301...At a certain moment of the matrix, the numeric content ends and I have several consecutive rows all composed of NaN till the end of the matrix. I attach here the code I am using:
v_cutin=2.5;
v_cutout=15;
v_rated=5.5;
v=linspace(v_cutin,v_cutout,15);
D=2.5;
R=D/2;
r=linspace(0.15*R,R*0.97,100);
omega_rpm=linspace(1,600,200);
dataset_red2_lin=NaN*ones(length(omega_rpm)*length(v)*length(r)+length(omega_rpm)*length(v)-1,7);
row_2=0;
i_eff=0;
for i=1:length(dataset2_lin)
row_2=row_2+1;
if row_2<=100
i_eff=i_eff+1;
dataset_red2_lin(i,:)=dataset2_lin(i_eff,:);
elseif row_2>100
dataset_red2_lin(row_2,:)=NaN*ones(1,7);
row_2=0;
end
end
save ('Folder path','dataset_red2','-ascii','-tabs');
In the attachment tyou can find the initial matrix dataset2 in the folder named "DATA" and the resultant txt file in the folder named "RESULT" .
Walter Roberson
2021년 12월 1일
Would the receiving program just happen to also be able to read xls or xlsm files? Getting blanks into those is easier (numeric nan are converted to emptiness in those.)
fmt = "%.7g"; %important that it is string not character vector
writematrix(standardizeMissing(compose(fmt, A), "NaN"), 'dataset_red2.txt', 'delimiter', 'tab')
Adjust the fmt as needed.
In the case where it would be acceptable to use 5 digits of precision, then you can use
writematrix(string(A), 'dataset_red2.txt', 'delimiter', 'tab')
Emilio Pulli
2021년 12월 2일
Unfortunately the receiving program is Tecplot. And the user of the receiving program asked me for a txt file with the afore mentioned features. As soon as I work on this, I will try your code and let you know if it works. Thank you man!
Emilio Pulli
2021년 12월 2일
편집: Emilio Pulli
2021년 12월 2일
Ok perfect, it works! Really really thank you!
The last doubt: do you know how to replace a number that has a lower number of digits with respect to the other numbers in the matrix with the same number with zero digits in order to have the same format of the other numbers? For instance, if I have a “1” in a matrix where all the other numbers are decimal with 4 decimal digits, how can I transform it into “1.0000”?
Walter Roberson
2021년 12월 2일
Use the version with compose and fmt
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
