Remove NaN from matrix
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi All
this is my code:
% Read the CSV file into a table
Data = readtable(L2);
% Define the desired oridN numbers and phase
desired_oridN = [1, 3, 7, 16, 19, 21, 22, 24, 27, 28];
desired_phase = 'P';
% Initialize cell arrays to store Azimuth values for each oridN
all_azimuths = cell(1, length(desired_oridN));
% Iterate over all rows in the table
for i = 1:height(Data)
% Check if the current row's oridN is in the desired list
idx = find(desired_oridN == Data.oridN(i));
if ~isempty(idx) && strcmp(Data.phase{i}, desired_phase)
% Store Azimuth value for the corresponding oridN
all_azimuths{idx} = [all_azimuths{idx}; Data.Azimut(i)];
end
end
% Convert cell array to matrix
max_length = max(cellfun(@numel, all_azimuths));
azimuth_matrix = NaN(max_length, length(desired_oridN));
for i = 1:length(desired_oridN)
azimuth_matrix(1:numel(all_azimuths{i}), i) = all_azimuths{i};
end
i get NaN values in each columns because my columns not in the same length and this is ok. How can i evoid this?
Thank you!!
댓글 수: 0
답변 (1개)
Voss
2024년 6월 5일
In a matrix, all columns are the same length. That's one of the defining characteristics of matrices. You're not going to be able to have a matrix whose columns are different lengths.
If you want to store column vectors of different lengths in a matrix, then you'll need to use NaNs or some other placeholder value to fill out the columns and make them all the same length in the matrix.
If you want to store column vectors of different lengths without any placeholder values, then store them in a cell array, exactly as you already have in your all_azimuths variable.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!