copying specific elements of an array to another.
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Leon Ellis
am 21 Aug. 2021
Beantwortet: Stephen23
am 21 Aug. 2021
Hi, I want to copy certain values from an array with a known size to an array with an unspesified size based on the critaria.
for i=1:length(y)
if abs(y(i))>0.001
y(i)=g(i);
end
end
So basically, I want to get all values in the array y that are greater than 0.001 to be copied into a seperate array g, but I'm struggling. Thanks in advance!
0 Kommentare
Akzeptierte Antwort
Stephen23
am 21 Aug. 2021
Forget about loops, the simple and efficient MATLAB approach is to use logical indexing:
g = y(y>0.001)
0 Kommentare
Weitere Antworten (1)
Awais Saeed
am 21 Aug. 2021
y = reshape(randn(3)/100,1,[]);
idx = 0;
for col = 1:1:size(y,2)
if (y(col) > 0.001)
idx = idx+1;
g(idx) = y(col); % store values > 0.001 in g
end
end
fprintf('y: ')
fprintf('%f ', y) % original data
fprintf('\ng: ')
fprintf('%f ', g) % values > 0.001
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!