Matlab find index of matched elements and replace values
Afficher commentaires plus anciens
I want to create an array of indices using Matlab's find-function. Here is what I need to do:
I have an array a1 containing time data in the datenum format (incomplete set) and an array v1 (same length as a1) containing some values.
I now created a new array a2 also containing time data in the datenum format (this time complete set, hence length(a2) > length(a1)) and initialized an array v2 with zeros with length(a2).
What I want to do is to replace the zeros in v2 with the data in v1 where a1 matches a2.
I think this is a case for indexing, where I ultimately want to do the following:
v2(ind) = v1; % whereas ind contains the indices of the matched elements of a1 and a2
However when I try to create an array ind to store the indices where a1 matches a2 I get an error related to the dimensions:
ind = find(a1==a2);
Error: Matrix dimensions must agree
Starting point:
a1 = [2;3;4;6;9]; % simplified time-vector ("incomplete")
v1 = [1;2;1;1;2]; % data for each time-point in a1
a2 = [1;2;3;4;5;6;7;8;9;10]; % "complete" time-vector
v2 = zeros(length(a2),1); % initialize final output variable
Desired Outcome:
v2 = [0;1;2;1;0;6;0;0;2;0] % values of v1 inserted where a1 matches a2
Can someone help me out here?
Many thanks in advance!
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!