Insert missing values in specified positions and use for loop

2 Ansichten (letzte 30 Tage)
Hi everybody could anyone help me in my problem: i have an array with known missed values in known places . I want to insert these values in the specified positions and do it for many arrays. Example a=[2 4 6 3 3 5 3 4 1 0 0 0 0] Li=[2 8 10 12] %locations of values in array Val=[6 7 8 9] Solution should be Sol=[2 6 4 6 3 3 5 7 3 8 4 9 1] Also how to apply this in for loop for multiple arrays Thanks in advance

Akzeptierte Antwort

KSSV
KSSV am 25 Jun. 2020
This problem is dicussed multiple times....you can checki there in this useful link:
  5 Kommentare
KSSV
KSSV am 26 Jun. 2020
a = [1 2 3 4 5 6 7 8 9; 3 4 1 2 5 6 7 2 8; 9 8 1 2 5 6 7 2 3];
val = [ 6 7 8 9] ;
col = [2 8 10 12] ;
b = [1 6 2 3 4 5 6 7 7 8 8 9 9; 3 6 4 1 2 5 6 7 7 8 2 9 8; 9 6 8 1 2 5 6 7 7 8 2 9 3] ;
[m,n] = size(a) ; p = length(col) ;
iwant = zeros(m,n+p) ;
for i = 1:m
iwant(i,col) = val ;
% Make logical indices
idx = logical(zeros(1,n+p)) ;
idx(col) = 1 ;
iwant(i,~idx) = a(i,:) ;
end
Hamdia Mansour
Hamdia Mansour am 28 Jun. 2020
Thank you very much for your help

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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!

Translated by