Remove NaN from matrix

6 次查看(过去 30 天)
israel cohen
israel cohen 2024-6-5
回答: Voss 2024-6-5
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!!

回答(1 个)

Voss
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.

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by