How to extract a part of a given vector and store it in a new vector using loops (for loop) ?

15 Ansichten (letzte 30 Tage)
Given vector: [1 2 3 4 5 6 7 8 9 10];
New vector, let's say: [4 5 6 7].
  2 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 21 Okt. 2013
Bearbeitet: Azzi Abdelmalek am 21 Okt. 2013
Is it homework? If yes, what have you tried so far?
Zack Shane
Zack Shane am 21 Okt. 2013
Bearbeitet: Azzi Abdelmalek am 21 Okt. 2013
This is my attempt:
seg1 = input('Enter the lower limit of the segment: ');
seg2 = input('Enter the higher limit of the segment: ');
ran = seg2-seg1+1;
while seg1<seg2+1
data2 = data1(seg1);
seg1 = seg1+1;
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

sixwwwwww
sixwwwwww am 21 Okt. 2013
Dear Abhisek, here is the code for it:
a = [1 2 3 4 5 6 7 8 9 10];
indices_of_values2save = [4 5 6 7];
count = 1;
for i = 1:length(a)
if any(i == indices_of_values2save)
b(count) = a(i);
count = count + 1;
end
end
I hope it helps. Good luck!

Weitere Antworten (1)

Image Analyst
Image Analyst am 21 Okt. 2013
Bearbeitet: Image Analyst am 21 Okt. 2013
data2 = data1(seg1:seg2); % Extract indexes seg1 through seg2 into new vector.

Kategorien

Mehr zu Matrix Indexing 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