Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

How to re-size a vector with new element obtained from old ones?

2 Ansichten (letzte 30 Tage)
Ayob
Ayob am 20 Apr. 2013
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
I need to create a new vector which is re-sized type of a vector and it's elements are obtained from previews vector.
for example:
V1=[1 3 9 11]
V2=[3*(1)-2,3*(1)-1,3*(1)-0,3*(3)-2,3*(3)-1,3*(3)-0,3*(9)-2,3*(9)-1,3*(9)-0,3*(11)-2,3*(11)-1,3*(11)-0]
As you can see vector V2 is made of vector V1. Each element of vector V1 in a special mode.
How can I write a code that build up V2 from V1 and be fast at the same time because I should do it several times and time is important to me.
  2 Kommentare
Image Analyst
Image Analyst am 20 Apr. 2013
What's wrong with the way you're doing it? Just replace 1 by V(1), 3 by V(2), 9 by V(3), and 11 by V(4). Should be faster than a rocket powered, greased cheetah on steroids.
Ayob
Ayob am 2 Mai 2013
It's ugly and bring about much difficulty to check it when you have a large program like mine.

Antworten (2)

per isakson
per isakson am 20 Apr. 2013
This is my best guess:
V1=[ 1, 3, 9, 11 ];
V2 = [ 3*V1(1)-2, 3*V1(1)-1, 3*V1(1)-0 ...
, 3*V1(2)-2, 3*V1(2)-1, 3*V1(2)-0 ...
, 3*V1(3)-2, 3*V1(3)-1, 3*V1(3)-0 ...
, 3*V1(4)-2, 3*V1(4)-1, 3*V1(4)-0 ];
>> V2
V2 =
1 2 3 7 8 9 25 26 27 31 32 33
I submit this to learn if I'm correct so far. Can V1 take any length?
  1 Kommentar
Zhang lu
Zhang lu am 2 Mai 2013
Hi. what about
V1=[1 3 9 11];
a=[-2,-1,0];
V2=(repmat(3*V1',1,length(a))+repmat(a,length(V1),1))';
V2=V2(:)'

Jan
Jan am 2 Mai 2013
Bearbeitet: Jan am 2 Mai 2013
V1 = [1, 3, 9, 11];
a = [2, 1, 0];
V2 = reshape(bsxfun(@minus, V1, a(:)), 1, []);

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by