Clear Filters
Clear Filters

Remove NaN from matrix

6 views (last 30 days)
israel cohen
israel cohen on 5 Jun 2024
Answered: Voss on 5 Jun 2024
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!!

Answers (1)

Voss
Voss on 5 Jun 2024
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.

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!