matlab triks

Upload: elie-feghali

Post on 07-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Matlab Triks

    1/53

    ?

    blinkdaggeran Engineering and MATLAB blog

    Home About

    MATLAB

    Contact Us

    Listchecker

    Matlab Tips and Tricks on Manipulating 1-D Arrays(Vectors)14 Jan 2008Quan Quach196 comments

    Introduction1-D Arrays (also known as vectors) are commonly used within Matlab, so it is agood idea to understand how they work and how to bend them to your will. This isa quick tutorial on some simple tricks that you may or may not know about vectors.

    Creating Vectors1. How to create a row vector that increments by 1. For example, lets create a row vector

    that goes from 1 to 10, with increments of 1.

    2. myVector = [1 2 3 4 5 6 7 8 9 10]; %the hard way3. myVector = 1:10 %the easy way

    4. How to transform a row vector to a column vector, and vice versa.5. myVector = 1:10; %creates a row vector6. myVector = myVector' %this is the complex conjugate transpose7. myVector = myVector.' %is the non-conjugate transpose

    Note: Dan Kominsky pointed out that is a subtle but important difference here. When youare working with real numbers the difference is irrelevant, but when you are dealing withcomplex numbers, the meaning is entirely different! Thanks for the correction, Dan.

    8. How to create a column vector that increments by 1. For example, lets create a columnvector that goes from 1 to 10, with increments of 1.

    9. myVector = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]; %the hard way10.myVector = [1:10].' %the easy way

    11. How to create a vector that increments by a specific value. Lets create a vector that goesfrom 1 to 19, and increments by 2. Note that the increment value is not limited tointegers.

    12.myVector = 1:2:19

    13. How to create a vector that decrements by a specific value. Lets create a vector that goesfrom 10 to 1, and decrements by 1

    14.myVector = 10:-1:1

    http://blinkdagger.com/http://blinkdagger.com/http://blinkdagger.com/about/http://blinkdagger.com/matlab/http://blinkdagger.com/contact-us/http://blinkdagger.com/install-listchecker/http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://blinkdagger.com/author/misterturtie/http://blinkdagger.com/http://blinkdagger.com/about/http://blinkdagger.com/matlab/http://blinkdagger.com/contact-us/http://blinkdagger.com/install-listchecker/http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://blinkdagger.com/author/misterturtie/http://blinkdagger.com/
  • 8/4/2019 Matlab Triks

    2/53

    15. How to create a vector with equally spaced points. Lets create a vector that goes from 0to 100 with 21 equally spaced points.

    16.%first argument is the start value of the vector17.%second argument is the end value of the vector18.%third argument is the number of points within the vector19.myVector = linspace(0,100,21)

    20. How to create a vector of zeros. For example, lets create a vector of 10 zeros.

    21.%first argument is the number of rows22.%second argument is the number of columns23.rowZeros = zeros(1,10)

    Note: Incidentally, this is a great way to preallocate a vector. Preallocating a vector ismost useful when FOR loops are involved. Preallocating a vector is preferred overresizing a vector repeatedly as it reduces the processing time.

    24. How to create a vector of ones. For example, lets create a vector of 10 ones.

    25.%first argument is the number of rows26.%second argument is the number of columns

    27.rowOnes = ones(1,10)Note: This is yet another way to preallocate a vector.

    Adding, Removing, and Replacing Elements within aVector

    28. How to append a vector. For example, lets add 11 to the end of the vector

    29.myVector = 1:10;30.myVector = [myVector 11]31.32.%we can also add 11 to the beginning of the vector33.myVector = [11 myVector];

    Note: This method of appending vectors should not be used within large FOR loops.When resizing arrays, memory must be reallocated with a larger size. If this is donerepeatedly, there is a speed penalty.

    34. How to append two vectors together.

    35.myVector1 = 1:5;36.myVector2 = 6:10;37.myVectorAppend = [myVector1 myVector2]38.%myVectorAppend = cat(2,myVector1,myVector2) does the same thing

    Note: Same warning as above.

    39. How to remove a particular element from a vector. Lets say we want to remove the 4th

    entry.40.myVector = 1:10;41.myVector(4) = []

    42. How to replace a particular element with a different element within a vector. Lets say wewant to replace the 4th entry with the value of 100.

    43.myVector = 1:10;44.myVector(4) = 100

  • 8/4/2019 Matlab Triks

    3/53

    45. How to remove the last element from a vector.

    46.myVector = 1:10;47.myVector(end) = []

    48. How to remove the last 5 elements.

    49.myVector = 1:10;

    50.myVector(end-4:end) = []

    51. How to keep the last 5 elements (or equivalently, remove the first five elements).

    52.myVector = 1:10;53.myVector = myVector(end-4:end)54.55.%the following command does the same thing56.myVector(1:5) =[];

    57. How to remove a series of elements. For example, lets remove entries 3 through 6:

    58.myVector = 1:10;59.myVector(3:6) = []

    60. How to keep a series of elements. For example, lets keep entries 3 through 6:

    61.myVector = 1:10;62.myVector = myVector(3:6)

    63. How to remove a group of specific elements. For example, lets remove entries 2,5, and 7:

    64.myVector = 1:10;65.myVector([2,5,7]) = []

    66. How to keep a group of specific elements. For example, lets keep entries 2,5, and 7:

    67.myVector = 1:10;68.myVector = myVector([2,5,7])

    69. How to get the number of elements within a vector. Useful when creating a for loop torun through a vector.

    70.myVector = 1:10;71.numElements = length(myVector)72.73.%the following command does the same thing74.numElements = numel(myVector)

    75. How to remove all zeros from a vector.

    76.myVector = [0 0 0 1 2 3 0 0 4 5 1 2 0 0];77.78.%index contains the indices elements within myVector which are non-zero79.index = find(myVector);80.myVector = myVector(index) %removes all the zeros within the vector

    Alternatively, logical indexing can be used (and is more efficient)myVector(myVector == 0) = [];

    81. How to remove a particular value from a vector. For example, how to remove anyoccurence of 6 within a vector

    82.myVector = [6 6 0 1 2 3 0 0 6 6 1 2 0 0];83.84.%index contains the indices of elements within myVector which are equal

    to 6

  • 8/4/2019 Matlab Triks

    4/53

    85.index = find(myVector == 6 );86.myVector(index) = []

    Alternatively, logical indexing can be used (and is more efficient)

    myVector(myVector == 6) = [];

    87.How to remove the first two occurences of 6 within a vector

    88.myVector = [6 6 0 1 2 3 0 0 6 6 1 2 0 0];89.index = find(myVector == 6,2);90.myVector(index) = []

    91. How to remove all elements greater than 5 from a vector.

    92.myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];93.94.%index contains indices of elements within myVector which are greater

    than 595.index = find(myVector > 5);96.myVector(index) = []

    Alternatively, logical indexing can be used (and is more efficient)

    myVector(myVector > 5) = [];

    97. Similarly, how to remove all elements less than 5 from a vector.

    98.myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];99.100.%index contains the indices of elements within myVector which are less

    than 5101.index = find(myVector < 5);102.myVector(index) = []

    Alternatively, logical indexing can be used (and is more efficient)

    myVector(myVector < 5) = [];

    Sorting and Shifting Vectors103.How to reverse a vector.

    104.myVector = 1:10;105.myVector = myVector(end:-1:1)

    106.How to sort a vector.

    107.myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];108.myVectorAscend = sort(myVector) %sort ascending109.myVectorDescend = sort(myVector,'descend') %sort descending

    110.How to shift the elements one spot to the right.

    111.myVector = 1:10;

    112.myVector = myVector([end 1:end-1])113.How to shift the elements one spot to the left.

    114.myVector = 1:10;115.myVector2 = myVector([2:end 1])

    Other useful commands:116.How to get the maximum and minimum value of a vector

    117.myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];

  • 8/4/2019 Matlab Triks

    5/53

    118.maxValue = max(myVector);119.minValue = min(myVector);

    120.How to add up all the elements within a vector.

    121.myVector = 1:10;122.total = sum(myVector);

    123.How to get the product of all the elements within a vector.124.myVector = 1:10;125.total = prod(myVector);

    126.How to get the average, standard deviation, and variance of a vector.

    127.myVector = 1:10;128.averageArray = mean(myVector)129.stdArray = std(myVector)130.varArray = var(myVector)

    Got any tricks up your sleeve?There are a ton of Matlab tricks that were not covered in this tutorial. Do you know of any super

    useful trick? If you have any of your own tips and tricks up your sleeve, please share at least one!

    196 Responses to Matlab Tips and Tricks on Manipulating 1-D Arrays(Vectors)

    1. on 14 Jan 2008 at 7:46 am 1Dan K

    Im sorry to tell you that you made one of the classic blunders of matlab programming.The transpose of a vector is NOT myVector ! It is myVector. When you are workingwith real numbers the difference is irrelevant, but the myVector is the complexconjugate of the transpose! And believe me it is a royal pain to debug that mistake, since

    it is a coding error that is very hard to see. Hope this helps.Dan

    2. on 14 Jan 2008 at 2:14 pm 2Quan Quach

    Dan K:

    Thanks for the correction. It is a subtle, but important point!! I have edited the post toreflect your comment.

    Thanks,

    Quan

    3. on 15 Jan 2008 at 7:31 am 3Dan K

    Actually, Quan, there is one other major area in which your code is inefficient (althoughfunctionally correct). It is generally significantly faster to use logical indexing rather thanthe find command. For example in item number 24 you use:index = find(myVector > 5);myVector(index) = []

    Whereas a faster implementaiton (and, I think more clear) is:

    myVector(myVector >5)=[];

    http://quach.blinkdagger.com/http://feeds.feedburner.com/Blinkdaggerhttp://reddit.com/submit?url=http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://digg.com/submit?phase=2&url=http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://www.technorati.com/faves?add=http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://www.stumbleupon.com/submit?url=http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://del.icio.us/post?url=http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://quach.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    6/53

    HTH,Dan

    4. on 15 Jan 2008 at 7:56 pm 4Quan Quach

    Thanks Again Dan. Your suggestion is duly noted and I have edited the post to reflect it.

    5.on 21 Jan 2008 at 2:40 pm 5VahidI need to muliple a vector to every single column (as the same size of the vector) of amatrix element by element without using the for loop. Is there any way to do it?

    6. on 21 Jan 2008 at 3:23 pm 6Quan Quach

    Try using the repmat command. For example:

    myVector = [1;2;3;4] %the vector you will be multiplying with

    myMatrix = magic(4); % the matrix you are multiplying into.

    myVectorRepeated = repmat(myVector,1,4);

    myAnswer = myMatrix .* myVectorRepeated; %notice the dot before themultipy

    7. on 16 Feb 2008 at 9:52 am 7Dave

    I visited your website and i found it very useful. Dan comments were even better. Greatwork!

    8. on 09 Mar 2008 at 2:43 pm 8Percy

    Ive got a question about removind some indices from a cell, e.g.

    a=cell(10,10);indicesToRemove = [3,5,8];

    I want to remove both the 3th,5th and 8th columns AND rows.

    such that the dimension of a is [7,7]

    it doesnt work to put these to a{ind,:}={}; a{:,ind}={}; or does it?

    9. on 09 Mar 2008 at 3:05 pm 9Quan Quach

    Percy,

    Try this:

    a(:,indicesToRemove) = [];a(indicesToRemove,:) = [];

    10.on 12 Mar 2008 at 3:06 am 10anuj

    My question is -

    How can I remove rows in a matrix that have third element in the column 0

    Input-1 2 32 3 00 2 30 2 0

    http://quach.blinkdagger.com/http://quach.blinkdagger.com/http://quach.blinkdagger.com/http://www.dobbelsteyn.nl/percyhttp://www.dobbelsteyn.nl/percyhttp://quach.blinkdagger.com/http://quach.blinkdagger.com/http://quach.blinkdagger.com/http://quach.blinkdagger.com/http://www.dobbelsteyn.nl/percyhttp://quach.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    7/53

    Answer should be-1 2 30 2 3

    11.on 26 Mar 2008 at 12:19 am 11jack

    if I have two vectors that have different length,eg.

    vector1 a=[1 2; 0 1; 2 1] and vector2 b=[ 1 2; 2 1]and I want of delete the elements from vector1 a that have the same values with vector2b?

    without using loop, how to make a program?

    12.on 26 Mar 2008 at 12:21 am 12jack

    assuming that I just have the vectors of a and b, but doesnt know the index of b in a.

    13.on 26 Mar 2008 at 12:45 am 13Daniel Sutoyo

    hey jack you can use the command

    setdiff(a,b,rows)

    14.on 27 Mar 2008 at 9:49 pm 14takkari

    Hi. I am coverting R code into Matlab and have no idea how to do the following:

    >y z order(y)[1] 1 3 8 2 5 4 6 7> p p[1] 0.80 3.00 0.30 0.00 0.20 1.30 9.10 0.09

    How can I implement this code in matlab as there is no order() function?

    15.on 27 Mar 2008 at 11:25 pm 15Quan Quach

    Takkari,

    Maybe the sort function is what you are looking for.

    Quan

    16.on 28 Mar 2008 at 11:32 am 16takkari

    Hi Quan,sort(y) just sort the data.What I need is a vector containing indices of the sorted values in original vector y.

    17.on 28 Mar 2008 at 2:19 pm 17Daniel Sutoyo

    Takkari,

    You could have said that in the first place, Instead of showing us the R code.

    [y,i] = sort(y)

    y is your new sorted valuesi will be you indices

    18.on 28 Mar 2008 at 2:19 pm 18takkari

    This code do the same job but takes much time:x=sort(y);

    http://innovate.blinkdagger.com/http://innovate.blinkdagger.com/http://quach.blinkdagger.com/http://quach.blinkdagger.com/http://innovate.blinkdagger.com/http://innovate.blinkdagger.com/http://innovate.blinkdagger.com/http://quach.blinkdagger.com/http://innovate.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    8/53

    p=[];for i=1:length(x)

    p=[p find(y==x(i))];

    endlength of x is 392480

    19.on 28 Mar 2008 at 3:04 pm 19takkari

    Thanks Daniel. Problem solved.

    20.on 31 Mar 2008 at 1:51 pm 20takkari

    Hi Dear,The following code is taking much time any idea how to make it run faster?omegac,alphac,betac are scalars. h,Y,b,bb are vecotrs of of length n.

    for t=3:nh(t)=omegac+alphac*Y(t-1)+betac*h(t-1);b(t)=Y(t-1);

    bb(t)=(omegac/(1-betac)^2);for j=2:(t-1)b(t)=b(t)+((betac)^(j-1))*Y(t-j);bb(t)=bb(t)+alphac*(j-1)*((betac)^(j-2))*Y(t-j);endend

    Thanks

    21.on 31 Mar 2008 at 3:33 pm 21Quan Quach

    Takkari,

    Heres something to get you started.

    Quanh(3:n) = omegac + alphac*Y(2:n-1) + betac*h(2:n-1);b(3:n) = Y(2:n);bb(3:n)=(omegac/(1-betac)^2);

    for t=3:nfor j=2:(t-1)b(t)=b(t)+((betac)^(j-1))*Y(t-j);bb(t)=bb(t)+alphac*(j-1)*((betac)^(j-2))*Y(t-j);endend

    22.on 28 May 2008 at 12:50 pm 22Jon V

    Hey! I just wanted to say that your MATLAB tutorials are great, I found them extremelyhelpful. Everyone elses comments were helpful as well. You have a great website!

    Thanks

    23.on 06 Jun 2008 at 11:34 am 23sathya

    n=1:10;average=mean(n)

    http://quach.blinkdagger.com/http://quach.blinkdagger.com/http://quach.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    9/53

    ??? Error: File: mean.m Line: 2 Column: 1Function definitions are not permitted at the prompt or in scripts.

    I got this error.How can i get mean?

    24.on 06 Jun 2008 at 11:49 am 24sathya

    How about these?n(isprime(n))=0 returns 0 for all the primevalues of nn(rem(n,2)==0)=0 returns 0 for all the even values of nn(rem(n,2)~=0)=0 returns 0 for all the odd values of n

    25.on 08 Jun 2008 at 3:36 pm 25Travis Johnson

    I recently found the unique(V) function very useful in a permutations algorithm whichgenerated the correct answer several ways and stored them all. To prune it down, I usedunique.

    >> v=[0 0 1 1 2 2];>> unique(v)ans =

    0 1 2

    26.on 25 Jun 2008 at 8:03 am 26Ashley

    I have a matrix and I want to remove all columns that are zeros. Does anyone know howto do that?Thanks!

    27.on 14 Jul 2008 at 5:19 pm 27Mina

    Hey All,

    Thanks for the useful article.

    I need to access a 2-D array as a LUT.

    example

    LUT = [11 12 13 14; 21 22 23 24; 31 32 33 34];

    rowIndex = [3 2]; columnIndex = [4 1]; % size of rowIndex equal size columnIndex

    Can I get a vector of (3,4) & (2, 1) in a single command.

    result = [34 21];

    28.on 14 Jul 2008 at 5:29 pm 28Mina

    I got it

    We can use

    result = LUT(sub2ind(size(LUT), rowIndex , columnIndex ));

    29.on 24 Jul 2008 at 5:48 am 29tanuj

    Hey thanks for nice tips.I have a 2-D matrixA = [1 2 3;4 5 6]; (A can be any arbitary matrix)i want to sum all the elements above the value 2. How can i do that ?i mean i want 3+4+5+6=18(answer)

    http://www.traviscj.com/http://www.traviscj.com/http://www.traviscj.com/
  • 8/4/2019 Matlab Triks

    10/53

    Thanks a lot

    30.on 24 Jul 2008 at 6:10 am 30tanuj

    hey i guess i got the answer

    A = [1 2 3; 4 5 6];

    % if i want to add all the values greater than 3, thena(a

  • 8/4/2019 Matlab Triks

    11/53

    B = [5 6 7 8];C = [9 10 11];

    (note the unequal dims). And Id like to have a code that gives the following result:

    Result = [1; 5; 9; 2; 6; 10; 3; 7; 11; 4; 8];

    Thanks in advance for any help!

    35.on 23 Aug 2008 at 8:51 pm 35Quan Quach

    Hi Baba,

    Here is my idea:

    %first, pad all vectors using NaN so that they are the same lengthC = [9 10 11 NaN];

    %second, put all vectors into matrix formtemp = [A;B;C];

    %third use the reshape function

    Result = reshape(temp,numel(temp),1);

    %fourth, remove all NaNs

    Result(isnan(Result)) = [];

    Quan

    36.on 20 Oct 2008 at 5:19 pm 36asur010

    Hi Quan,

    I find your tips very useful and to the point. Thanks for that..

    I want to know of a way to find a particular element and its corresponding index from a

    vector if it is greater than something. The find function usually returns all values whichare greater than it, how can I just get the first number which is greater than the specifiedvalue.

    For example I have a vector = [40,50,60,60,70,80,90,91,92,93];I want to find the first element which is >90 and its corresponding index in the originalvector.If I say find (vector>90) it gives all numbers >90.

    Could you help out please?

    Thanks.

    Adi

    37.on 05 Nov 2008 at 4:50 pm37Anonymous

    Hi asur,u can use

    for i=1:numel(vector)if vector(i) > 90index=i;break

    http://quach.blinkdagger.com/http://quach.blinkdagger.com/http://quach.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    12/53

    endend

    38.on 13 Nov 2008 at 6:32 am38Graham

    Hi,

    I wonder if there is a built in function that would give me the number of times k appearsin a vector ? E.g. vec = [1 1 1 2 2 3 3 4 ] where k == 1ans = 3

    39.on 14 Nov 2008 at 5:44 am39Graham

    sorry 4got to add,

    Hi,

    I wonder if there is a built in function that would give me the number of times k appearsin a vector without using the hist function. ? E.g. vec = [1 1 1 2 2 3 3 4 ] where k == 1ans = 3

    40.on 16 Nov 2008 at 1:27 pm40wladmir

    Thank you very much for your help and for your time!!! thank you very much!!!

    41.on 22 Dec 2008 at 3:03 pm 41Sathya

    Hi asur,you can use,

    find(vector >90,1)to get the index of the first number greater than 90 in the vector.

    42.on 23 Dec 2008 at 12:06 am 42Zane Montgomery

    Hi Graham,You could use a very simple for loop to track how many times a number appears in

    your vector.soln=0;for n=1:size(vec,2) % gets the number of columns of the row vector

    if vec(n)==1soln=soln+1;

    elseend

    end

    43.on 23 Dec 2008 at 10:14 am 43Rob S.

    @Graham, Zane,

    OR, you could shoot the vectorizer at this problem. Something like:

    vec = [1 1 1 2 2 3 3 4 ] ;

    % if you want to count how many 1's

    sum(vec==1)

    Hope this helps! Cant find it right now, but there is a great post by Loren about indexingthat I think covers logicals like this. Also skim Steves blog for logical operations sincetheyre used in image processing a lot.

    http://none/http://none/http://none/http://none/http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.mathworks.com/matlabcentral/fileexchange/authors/29800http://www.mathworks.com/matlabcentral/fileexchange/authors/29800http://none/http://none/http://www.blinkdagger.com/http://www.mathworks.com/matlabcentral/fileexchange/authors/29800
  • 8/4/2019 Matlab Triks

    13/53

    Merry Christmas!

    Rob

    44.on 23 Dec 2008 at 9:39 pm 44Rob S.

    @ Tanuj,

    Since Im on a roll with vectorizing and using logical indexing, I see you had aninteresting problem farther up the page. If you need to sum up all the elements of A thatare greater than 3, try this:

    A = [1 2 3; 4 5 6];sum(A(A>3))

    Read the second line as sum up the elements of A only where the elements of A aregreater than 3

    I dont want to duplicate what Lorens and Steves blogs have covered about logicals, butthis method basically produces a mask vector of 1s and 0s the same size as A. It tellsthe sum function which elements to include and exclude from its calcs.

    I also like solutions based on logicals since they very often handle whole vectors at atime, and avoid using loops. Less mess for you, and usually faster code.

    HTH,Rob

    45.on 02 Jan 2009 at 3:55 am 45fawx

    hi

    how do i insert elements into an array by shifting the remaining elements to the right?

    eg .

    a=1:5

    now insert 8 and 9 between 2 and 3 such thata= 1 2 8 9 3 4 5

    thanks

    46.on 02 Jan 2009 at 9:37 am 46Rob S.

    @ fawx,

    Unfortunately, this will be a fairly manual operation. I would initialize a new 1D array(lets call it b) of size a + the inserts. Then fill it in with your values. Something like:

    % create arrays to start witha = 1:5;inserts = [8 9];

    b = zeros(numel(a)+numel(inserts),1);

    % now fill in your valuesb(1:2) = a(1:2);b(3:4) = inserts(:);b(5:end) = a(3:end);disp(b);

    HTH,Rob

    http://www.mathworks.com/matlabcentral/fileexchange/authors/29800http://www.mathworks.com/matlabcentral/fileexchange/authors/29800http://www.mathworks.com/matlabcentral/fileexchange/authors/29800http://www.mathworks.com/matlabcentral/fileexchange/authors/29800http://www.mathworks.com/matlabcentral/fileexchange/authors/29800http://www.mathworks.com/matlabcentral/fileexchange/authors/29800
  • 8/4/2019 Matlab Triks

    14/53

    47.on 02 Jan 2009 at 2:59 pm 47Zane Montgomery

    @fawx

    Robs method is extremely good for especially large arrays. Pre-assigning the size ofarrays is good practice to improve the processing speed.

    Another way, using vector concatenation would bea = 1:5;inserts = [8 9];

    % force whatever elements you want between segments of your 1-D array.b=[a(1:3) inserts a(4:end)]

    A few less lines, but can be harder for multi-dimensional arrays.

    p.s. Thanks for the help with logicals Rob

    48.on 02 Jan 2009 at 10:48 pm 48fawx

    Thanks a lot Rob S. and Zane Montgomery

    that really helped a lotfawx

    49.on 05 Feb 2009 at 8:14 pm 49rahul

    Hi Dan,Great tips !I am stuck with a problem related to sorting of a vector:I have following vector

    vector = [1 1 0 0 0 1 1 1 0 1 1 1 0]

    What I want to know is how are ones grouped together ?

    In above vector they are grouped as = 2, 3, 2

    How can I sort my vector this way as to reveal the groupings ..

    Thank you

    Rahul

    50.on 05 Feb 2009 at 8:16 pm 50rahul

    In above the ans should be

    ans = 2,3,3

    51.on 12 Mar 2009 at 9:42 pm 51mohammed

    hey great work Quan

    thanks a lotthis stuff is great

    52.on 02 May 2009 at 6:35 am 52Yo

    Hi guys,

    First let me say that I find this blog superb. Thank you for having it.

    My problem consist in averaging every 10 elements of an array (G) of size 100 andstoring those averages. Here is my solution:

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    15/53

    k = 100;G = randn(k,1);Gav=mean(reshape(G,10,k/10));

    This save me a lot of time, more than if I used a for loop instead, but still I would like toimprove it further if possible. Do you guys have another suggestion?

    Thank you.53.on 02 May 2009 at 9:52 am 53Matt

    Hello everyone, Im sorry if this has already been answered, but is there no built-infunction that returns the elements you remove from an array? Example:

    a = [1,11,111]

    I want to return element 2 and 3 to create an array b, leaving a with only its first elementintact:

    a == [1]b == [11,111]

    Any such operator or do I need to create it myself? Im not a programming guru so Imnot certain how to create this functionality in the most efficient way.

    Thanks in advance, Im sure someone has a great answer to a beginner such as myself ^^

    54.on 03 May 2009 at 11:18 pm 54nagendra

    to create logic gates by using matlab,program.

    55.on 05 May 2009 at 9:14 am 55Yo

    Hi Matt,

    What you could do is before removing some elements of a(:), store them in anothervector b(:). I solved your example here:

    a = [1,11,111];b=a(2:3);a(2:3)=[];

    You can do more complicated stuff, for instance if you want to remove the elements ofa(:) equal to 11:

    a = [1,11,111];b=a(a==11);a(a==11)=[];

    -Yo

    56.on 16 Jun 2009 at 5:25 am 56Johan, Jeppe

    We have a vector with some values given by a function. We want to duplicate this vector

    N times, so we get a 2 dimensional matrix.

    Eg,

    { 1 0 1 0 0 1 } Our vector

    After the operation we want this result:

    1 0 1 0 0 11 0 1 0 0 11 0 1 0 0 1

  • 8/4/2019 Matlab Triks

    16/53

    1 0 1 0 0 1...(N) times

    Our try:

    function Y = jail(X,p)

    [r,c] = size(X); %We want our duplicated array to be of this size..

    t = 1:1:c;M = sin((pi*t)/p).^2 ;F = repmat(M,r,c);Y = F;

    But this doesnt give us a 2 dimensional matrix.

    Thanks in advance

    57.on 16 Jun 2009 at 7:12 am 57Zane Montgomery

    @Johan&Jeppe,

    Im curious if the matrix youre getting is 1-D or 3-D+? I would think it should stilloutput as 2-D, just not the style that you want.

    Lets call your vector a. Since you want to make a new matrix that is a column vector ofmany a vectors, your repmat should be repmat(M,N,1) where N is the N times youwanted above (aka r in your code)

    function Y = jail(X,p)

    [r,c] = size(X); %We want our duplicated array to be of this size..

    t = 1:1:c; %1:c works too, by default it increments 1 every timeM = sin((pi*t)/p).^2 ;F = repmat(M,r,1); %changedY = F;

    This will stack the matrix/vector on top of itself r times. Hope that helps!-Zane

    58.on 17 Jun 2009 at 2:26 am 58Johan, Jeppe

    Thanks Zane for the quick response, this solved our problem.

    59.on 20 Jun 2009 at 5:46 pm 59Nick Iversen

    How do I return the, say, first element of an array expression without storing theexpression into a temporary variable first? Eg

    result = first(expression);

    % rather than

    tmp = expression;result = tmp(1);

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    17/53

    60.on 23 Jun 2009 at 8:53 pm 60Tariq

    Hi .. Its really usefull wesite.. I need to know how to get the average of group ofvectorsLets say we have:A=[1 3 5]B=[4 7 1]

    C=[8 9 0]is there anyway to get the average of A,B and C??

    61.on 23 Jun 2009 at 9:06 pm 61bluray

    I wanted to insert the zero column vector in the matrix. I have this matrix

    [1 0 0 10 0 1 11 1 1 00 1 0 1]

    How do I convert it to

    [1 0 0 0 0 0 1 0

    0 0 0 0 1 0 1 01 0 1 0 1 0 0 00 0 1 0 0 0 1 0]

    Thanks

    62.on 25 Jun 2009 at 12:43 pm 62Circshifter

    Hi Quan,

    Thanks for all of the tips. I HATED shifting vectors! Its not practical in most cases.Quan, you should check out the circshift command its easy and you just enter theammount that you want the vector shifted (you do however, have to convert the vectorinto a colum vector):

    u = [1:10];u1 = (circshift(u',3))'

    % This shifts vector "u" 3 places to the right, and places the lastthree elements% in the first three places!

    63.on 27 Jun 2009 at 10:19 pm 63nirveda

    hey,ive done a blunder by performing a set of operations on the wrong array, due to typingerror. The array content has now changed..i.e. its size has increased and values

    modified.Is there any way to rollback this to the original state? or do i have to recode byrote? theres really lengthy stuff preceeding it..thanks,nirveda

    64.on 30 Jun 2009 at 3:23 pm 64jose velez

    How would I create a 200 by 100 array

    size=250*100;time=1:size;

    http://www.nirveda.wordpress.com/http://www.nirveda.wordpress.com/http://www.nirveda.wordpress.com/
  • 8/4/2019 Matlab Triks

    18/53

    time=reshape(A,250,100)

    Nowhow would i filled the values of this array to[1 2 3 4 .....101 102 103...

    201 202 203]

    65.on 06 Jul 2009 at 3:13 pm 65sf

    jose velez:

    To create 200 x 100 array:

    myarray = ones(200,100);

    To fill it in with what you wanted:

    for index = 1:200myarray(index,:) = (index*100 - 99):(index*100);

    end

    There might be a better way than the for loop, but this will do the job quite nicely.

    66.on 12 Jul 2009 at 5:16 pm 66Jose

    The issue: I have two arrays filled with important data.Array1 is an array of size 31 x 31 arrayArray2 is an array of size 31 x 21

    I tried this but it only it only if the array is 1 dimensional.for i=1:9,t = [t 0];end

    Example

    array= [1 2;1 2;]how do i transform the previous array to look likearray =[1 2 0 0 0;1 2 0 0 0;]Keep in mind I cant do it manually it has to be by MATLAB code because is a 31 x 31array ( lots of numbers)Thank you for your efforts.

    67.on 14 Jul 2009 at 8:04 am 67Zane Montgomery

    Jose,

    Try this out:%Array1 is 31x31%Array2 is 31x21

    ArrayNew=[Array2 zeros(31,10)]

    This concatenates a zero array of size 3110 after you original 3121 array2. You canplay around with that, but it is very specific on dimensions of each array. This can also be

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    19/53

    expanded/parametrized for an MxN array > MxM array. Good luck!-Zane

    68.on 14 Jul 2009 at 9:53 am 68Jose

    That worked Zane you are a genius.

    69.on 15 Jul 2009 at 10:23 am 69Anonymous

    70.71.% hi guys, i'new @ matlab, this very good matlab site72.% i could need some help plz73.% i have wind speed data (winspd) & wind direction in different array74.% i want to calculate wind and direction data so i have north-south

    component75.% which contain wind speed and north (+)- south direction(-).76.77.%my array78.windspd = [1,2,3;4,5,6;7,8,9]79.winddir = [30,60,120;135,180,210;225,250,300]80.

    81.% code not running yet, i have problem how to calculate in array with 282.% variabel & calculate north-south direction in winddir so i havenorth-south wind speed component

    83.% north(0), east (90), south(180) & west (270)84.% correct me if i'm wrong with the if logic calculation i'm still new

    in matlab85.86.if winddir (find(winddir>=0 & winddir=90 & winddir=180 & winddir=270 &

    winddir

  • 8/4/2019 Matlab Triks

    20/53

    But even easier, MATLAB has built in capability to handle degree measurements:

    WinddirNS=cosd(winddir);% and you're done!

    cosd(),sind(),tand() are your functions if your angle is in degrees.

    And to get your desired solution:

    north_south = windspd.*WinddirNS;

    That got me what you were looking for, hopefully it works for you too.-Zane

    98.on 31 Jul 2009 at 1:51 am 72Budhie

    HI Zane,

    its works, thanks for the cool tips . yes i want to separete the wind component. but istill have problem doing operation in array.

    i want to do something like this :

    a = [1,2,3;4,5,6;7,8,9] %wind speedb = [30,60,120;135,180,210;225,250,300] % wind direction

    % i want to do operation between 2 array, but only on wind directionselected value and keep other value in array a that not selected.% how i can do that in matlab?

    if (b >=90)&(b=180)&(b=90)&(b=180)&(b

  • 8/4/2019 Matlab Triks

    21/53

    a = [1,2,2.59807;2.82842,-5,-5.19615;-4.94974,-2.73616,9]

    thx be4,

    -budhie-

    100.on 31 Jul 2009 at 1:55 am 74Budhie

    sry if double posting:Dif (b >=90)&(b=180)&(b=90)&(b=180)&(b=90) & (b=180)&(b

  • 8/4/2019 Matlab Triks

    22/53

    replaced by some value for e.g. all values greater than 3 and less than 7 be changed to 0in a vector.

    I will highly appreciate help in this regards,

    /Malaika.

    107.on 13 Oct 2009 at 4:53 am 81Anonymous

    I have 2 vectors say a = [1 2 3] and b= [4 5 6]. I want to do something like this

    c(1) = min( a(1) b(1), a(1) b(2), a(1) b(3));

    c(2) = min( a(2) b(1), a(2) b(2), a(2) b(3));

    c(3) = min( a(3) b(1), a(3) b(2), a(3) b(3));

    Can i do this without using loop with the help of array indexing or vectorising?

    108.on 20 Oct 2009 at 5:00 pm 82Tim

    hi,Is there a efficient way to replace all values in the array that is greater than 0 with 1s and

    rest with zeros while maintaining the dimensions of the array.e.g. a={ -1.2, 0.12, 1.5 , -0.009, 2} to a={ 0,1,1,0,1}

    great site by the way.

    109.on 21 Oct 2009 at 7:37 am 83Zane Montgomery

    Tim,

    Id recommend creating a new variable b to store you 0s/1s array, but other than that itseasy.

    a=[-1.2, 0.12, 1.5 , -0.009, 2];b=a>0; %note: zero gets mapped to a zero logical

    And youre done!2 quick notes:1) B is an array of logicals2) Make sure youre using array notation [ ], not cell notation { } The cells is anarray of 11 arrays and youll have to access each element individually to evaluate.

    Good luck,Zane

    110.on 09 Nov 2009 at 2:44 pm 84lokise+o

    @ malaika some pseudocode : you could try to get indicesfind occurences of first value and sav index array

    idx = find(args(1)) and loopadd + 1 after each cycle to the idxold+1 (6 13) => idxnew (7 14) and so ontest next argument like this (you can take length, sum, mean or numel of idx array)if mean(A(idxnew)) == val2 continue and increment pattern counterelse breakloop condition then would depend on flexibel vector length 3.4 5 2 testvector = vect(1:pos)hope it was helpful

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    23/53

    111.on 11 Nov 2009 at 3:40 am 85packiyaraj

    hi everyone ,

    This website is really useful for us. plz i need a help . How to group an array elementswithin range. plz help me.

    112.on 12 Dec 2009 at 6:25 pm 86Daithi

    Hi I am looking to convert a number to a 5 bit binary digit. Say 5 for example;

    >> dec2bin(5)

    ans =

    101

    I am looking for a 5 bit answer, 00101Any help is appreciated,Thanks

    113.on 15 Dec 2009 at 2:19 pm 87Zane MontgomeryDaithi,

    Try dec2bin(x,b) where x is your decimal number and b is the number of bits.

    GL,Zane

    114.on 03 Jan 2010 at 5:40 am 88krishh

    I have a array of dimension 5X16 ( 5 rows and 16 columns ) . I want to separate it into 2matrices having dimensions 5X8 and 5X8 with odd columns in one group and evencolumns in other group using matlab

    115.on 15 Jan 2010 at 11:14 pm 89hcu

    hii all..i hav a variable x=[1 3 4]i need to convert each value i.e. x(1,1), x(1,2), x(1,3) into binary and store in an arrayeg: Bin=[001; 011; 100].can anyone plz help me in writing d code for it in matlab??Thanks in advance!!i/p: [1 3 4]o/p:[001;011;100]

    116.on 27 Jan 2010 at 3:05 pm 90Murphy

    how do you remove all the even numbers from a vector?

    117.on 01 Feb 2010 at 11:43 am 91SPQR

    Murphey,

    a = [1 2 3 4 5 6]a(rem(a,2) == 1) = []

    The remainder function will flag all odd numbers with a 1, and then you simply askwhich are equal to one and destroy.

    118.on 01 Feb 2010 at 11:52 am 92SPQR

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    24/53

    krishh,

    Heres an example:a = [1 2 3 4;5 6 7 8]b = a(:, 1:2:end) % odd columnsc = a(:, 2:2:end) % even columns

    The b and c equations would remain the same for any size a.

    119.on 12 Feb 2010 at 1:39 pm 93Hank

    I have to vectors A and B. I need to form a new vector C that looks as follows.

    A=[1 3 5]B=[2 4 6]

    %Below is what C should beC=[1 2 3 4 5 6]

    120.on 12 Feb 2010 at 1:40 pm 94Hank

    I have to vectors A and B. I need to form a new vector C that looks as follows.

    A=[1 3 5]B=[2 4 6]

    %Below is what C should beC=[1 2 3 4 5 6]

    Thank you very much for you time

    121.on 17 Feb 2010 at 8:07 am 95SPQR

    Hank,

    Essentially do the opposite of my last example:

    A=[1 3 5]

    B=[2 4 6]

    C = zeros(1,length(A)*2); % initialize CC(1:2:end) = A % place As contents into C starting at 1, then 3, etc.C(2:2:end) = B % place Bs contents into C starting at 2, then 4, etc.

    And a correction to comment #91 I obviously removed all the odd numbers instead ofthe evens as asked.Change:a(rem(a,2) == 1) = []to:a(rem(a,2) == 0) = []this will flag the even numbers for removal.

    122.on 24 Feb 2010 at 2:18 am 96sangeeta

    [1 0 2 03 0 4 0]should becom[1 23 4]

    123.on 26 Feb 2010 at 8:54 am 97SPQR

    http://www.rediffmail.com/http://www.rediffmail.com/http://www.rediffmail.com/
  • 8/4/2019 Matlab Triks

    25/53

  • 8/4/2019 Matlab Triks

    26/53

    how can i make from the following line:5 5 5 5 5 6 6 6 6 7 7 7 8 8 9

    Matrix like this:

    1 5 5 5 5 50 1 6 6 6 6

    0 0 1 7 7 70 0 0 1 8 80 0 0 0 1 90 0 0 0 0 1

    Has anyone ideas?Thank you!

    129.on 26 Mar 2010 at 9:29 am 103ali

    how I can change the position of one element without changing the size of the matrix.

    x=[ 0 1 1 0 1]; to x= [ 1 1 0 0 1];

    Thanks130.on 26 Mar 2010 at 9:35 am 104ali

    how I can change the position of one element without changing the size of the matrix.x=[ 0 1 1 0 1]; to x= [ 1 1 0 0 1];Thanks

    131.on 28 Mar 2010 at 5:06 am 105waqas

    hi

    i want to remove the row of zeros for a matrix1 1 1 12 2 2 2

    0 0 0 05 5 5 5

    to1 1 1 12 2 2 25 5 5 5

    i tried too many way but couldnt find the right way.

    132.on 01 Apr 2010 at 2:12 pm106Zane Montgomery

    @aliIm not quite sure I know what youre getting at, but you could use something like:

    x=[ 0 1 1 0 1];x_new=[x(3) x(2) x(1) x(4:5)]

    This is very specific to this problem and gets trickier the more times elements need to beswapped. It works well for large segments of unchanged vectors like you see in the lastvector element. It could be x(4:500) if those are all unchanged which saves lots of time.

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    27/53

    @waqasThanks to a recently inspired video by Doug @TMW(http://blogs.mathworks.com/videos/) this solution is very easy.

    If a is your original matrix:

    a(3,:)=[]

    will remove the entire third row.

    enjoy!

    133.on 09 Apr 2010 at 4:46 am107Matt

    Hi, Ive got a problem.

    I define a matrix like this for exampel:

    >> j = [1,6,6;1,2,3;6,5,4]

    j =

    1 6 6

    1 2 36 5 4

    Now if I do this:

    length(j(j==6))

    I get 3 of course. What I want to do is count any number of occurences per row as oneoccurence, that is; even if one row contains 1, 2 or 3 sixes it should only return 1. Is thispossible? I hope it is clear what I want to do.

    >> j = [1,6,6;1,2,3;6,5,4;6,6,6;1,2,2;4,3,2]

    j =

    1 6 6

  • 8/4/2019 Matlab Triks

    28/53

    switched your j variable to a because j is a MATLAB variable for the imaginarynumber sqrt(-1)

    a = [1,6,6;1,2,3;6,5,4;6,6,6;1,2,2;4,3,2][row,col] = find(a==6);length(unique(row))

    I highly recommend you figure out how each line does what it does.Best,Zane

    136.on 14 Apr 2010 at 9:10 pm110Gumah

    hii want to add one column of zero whenever column of zero is exist1 0 1 1 0 12 0 2 2 0 23 0 3 3 0 35 0 5 5 0 5to1 0 0 1 1 0 0 12 0 0 2 2 0 0 23 0 0 3 3 0 0 35 0 0 5 5 0 0 5any body know how to do that ??

    137.on 30 Apr 2010 at 8:19 pm111emmi

    The code at entry number 24 is very nice. However if I have a matrix after applying this,the matrix becomes a row vector. How can I use this, and let the matrix stay as it is?

    24.How to remove all elements greater than 5 from a vector.

    myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];

    %index contains indices of elements within myVector which are greaterthan 5index = find(myVector > 5);myVector(index) = []

    138.on 30 Apr 2010 at 8:36 pm112emmi

    Lets say my matrix is sth like this:

    I wan to get rid of the zero lines. But I want to preserve the matrix. How can I do that?

    Secondly, if I have similar matrices in a multidimensional matrix, is there a difference?Lets say I have 3 of these matrices in A(:,:,1), A(:,:,2) and A(:,:,3) and I want to get ridof the zeros from all of these matrices.

    Thanks in advance for the help.

    -0.2498 -0.9092 0.3255 0.3564 0.7989 0.2441 37.7547292.3663

    -0.2333 -0.9699 0.3273 0.3449 0.7641 0.2366 34.5573304.3396

  • 8/4/2019 Matlab Triks

    29/53

    -0.1999 -0.9938 0.3223 0.3256 0.7279 0.2234 29.0925302.9826

    -0.2063 -0.9832 0.3317 0.3219 0.7204 0.2324 29.1262296.5975

    -0.2159 -0.9638 0.3346 0.3153 0.7109 0.2302 29.1044286.3701

    -0.2189 -0.9378 0.3334 0.3019 0.7017 0.2298 27.7276273.8610

    -0.2156 -0.9474 0.3348 0.2959 0.6668 0.2279 26.7218267.9965

    -0.2138 -0.9639 0.3315 0.2879 0.6314 0.2147 25.6377265.1570

    -0.2098 -0.9597 0.3267 0.2791 0.6331 0.2031 24.3022263.9478

    -0.2090 -0.9472 0.3249 0.2740 0.6209 0.1980 23.6842256.1645

    0 0 0 0 0 00 0

    0 0 0 0 0 00 0

    0 0 0 0 0 00 0

    139.on 03 May 2010 at 2:19 pm 113Zane Montgomery

    Emmi,

    the most recent posts in the comment area show this pretty well. Try those out and getback with any other questions.

    140.on 05 May 2010 at 1:43 pm 114Drew

    Hi,

    I am wanting to plot both of these files on the same graph for the functions below:x=0:0.1:3*pi;y1=sin(x+0.5);y2=90.*sin(x-0.5);what would go on this line to plot both files on the graph?

    Thanks in advance!

    141.on 09 May 2010 at 2:53 am 115sonny

    Hi,

    I have a question on removing multiple rows from a matrix. I knew we can remove rowsthat we want but specifying all rows index. But my matrix is quite big and how I canremove all the even row without specifying all the rows index?

    A=eye(100);indicesToRemove = [2,4,6,8,10, ......... 100]; %i want to make this flexible or how tomake a simple loop calling the indicesToRemove?

    The output A will have the size of [50x100]. Thanks in advance.

    142.on 10 Jun 2010 at 9:17 am 116fado

    hi,i want to remove multiple lines (7*i) index ,But my array is big (4461X1)

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    30/53

    can you help me plz?

    Thanks .

    143.on 13 Jun 2010 at 2:41 am 117jyoti sahu

    prob:[4 5 9 10 13 15 18 19 6 9 11 14 4 6 9 10 3 6 7 2 2]

    this is 1D array.answer should be [4 5 9 10 13 15 18 19 6 11 14 4 3 2]how can i reach this answer?

    144.on 28 Jun 2010 at 2:35 am 118Bertus

    @sonny/fado,I would do it like this:

    A=eye(100);for i=2:2:100A(i,:)=[];end

    145.on 28 Jun 2010 at 2:40 am 119Bertus

    oh btw, an easy way of converting a matrix to a vector:

    matrix=magic(5);vector=matrix(:);

    146.on 28 Jun 2010 at 10:01 am 120SPQR

    Sonny, Fado, and Bertus:

    You want to use logical indexing. Instead of using the for loop, as bertus did, yousimply index into the matrix all at once.

    Instead of

    A=eye(100);for i=2:2:100A(i,:)=[];end

    You directly call the rows and annihilate

    A = eye(100);A(2:2:end,:) = [];

    Keep in mind that my solution works for any size matrix to get rid of the even rows. Forodd rows you would write A(1:2:end,:). For columns you call them A(:,2:2:end). It keepsgoing for higher dimensions.

    Im not entirely sure about fados case whether he wants to annihilate every seventh row,or seven rows in a block. I would either call:

    A(1:7:end,:) = [];

    or

    A(1:7,:) = [];

    147.on 29 Jun 2010 at 1:40 pm 121Jason

    Greetings:

    http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d-arrays-vectors/
  • 8/4/2019 Matlab Triks

    31/53

    A=[1:0.1:4]';B=[1;1;1;1;5;5;5;5;1;1;1;1;1;1;7;7;7;7;1;1;1;1;1;1;1;1;1;1;1;1;1];

    C=A(find(A>=B));

    C of course is a vector of values in which A >= B:

    C =

    11.11.21.31.81.92

    2.12.22.3

    2.82.93

    3.13.23.33.43.53.63.73.83.94

    What I need however is to include the first element in the subsection that is not includedwith the find. So for example:

    C =

    11.11.21.31.3 %

  • 8/4/2019 Matlab Triks

    32/53

    3.13.23.33.43.53.63.73.83.94

    Im having some trouble explaining this so I hope it make sense. Please email if you canhelp. And great site by the way!!!

    148.on 30 Jun 2010 at 9:01 am 122Zane Montgomery

    yup, lost. dunno what your group and subsections are referring to.

    Can you make a smaller simplified example?

    149.on 30 Jun 2010 at 1:28 pm 123Jason

    find() of course returns the indicies that match, so in my example above:

    idx = find(A>=B);

    idy = find(A= than in B.

    What I need is to return the first index in each excluded group the first element in eachgroup where A < B. Call this situation X. So for example:

    idx =

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    33/53

    12345

  • 8/4/2019 Matlab Triks

    34/53

    C=A(A>=B);

    D = zeros(length(A),1);D(A>=B) = A(A>=B);D(D == 0 circshift(D == 0,1)) = A(D == 0 circshift(D == 0,1));D(D == 0) = [];

    So we create D equal in size to A -> place all logically true values into D, respectively ->find all logically false indexes (D == 0) and then use cirshift to find the leading edge,place leading-edge false data into D -> get rid of extra zeros

    152.on 13 Jul 2010 at 6:59 am 126Jason

    Wow, thanks guys. Both solutions work but like SPQR says, the second one is a bit morerobust for my purposes. Im still working to implement the code so Im not sure if it willwork as I hope (albeit the code provided here does EXACTLY what I asked for).

    Thanks.

    153.on 09 Aug 2010 at 12:42 pm 127Andre

    My questionHow can I get every data at every 5 numbers in a vector.

    For example:

    A =12345678

    9101112131415... (hundreds of numbers)

    And I have a data every 5 datas, like:Result:

    Result =1

    5913... (hundreds of numbers)

    Thanks a lot for all

    154.on 16 Aug 2010 at 8:39 am 128Zane Montgomery

    Andre,

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    35/53

    This can be done quite easily, you want to sample your column vector every n indexvalues. It looks like your solution starts at 1 and then gets every 4 after that, not 5.Thats an easy fix regardless.

    A=1:2000; %creates your vector from 1-2000 (as an example), same as1:1:2000sol=A(1:4:end); %starts at 1 and captures every 4th value after thatuntil end of vector A

    155.on 16 Aug 2010 at 10:14 pm 129anita

    Hi all,I need the matlab code for duplicate elimination in an arraykindly help me..i tried using the following code. couldnt get .

    clc;clear all;close all;a=[1 1 1 3 6 6 5 7 7 7 7 ];k=1;

    b(k)=a(1);

    l=numel(a);

    for i=2:l

    for j=1:kif a(i)==b(j)

    break;else

    k=k+1;b(k)=a(i);display(b);break

    end

    end

    end

    display(b);%insert code here

    Thank U..

    156.on 18 Aug 2010 at 10:45 am 130SPQR

    anita,a=[1 1 1 3 6 6 5 7 7 7 7 ];b = unique(a)

    157.on 01 Sep 2010 at 3:16 pm 131Dingo

    I have a one column vector that contains a series of azimuthal angles between 0 and 180

    ex.

    Azimuth=

  • 8/4/2019 Matlab Triks

    36/53

    0101562612056180etc

    I would like to replace all the values between 90 and 135 with 135.

    So if Azimuth>90 and Azimuth

  • 8/4/2019 Matlab Triks

    37/53

    x=v(:, :, 2)

    etc

    Just keep playing around with those and look at other comments/questions to hopefullyfind your answer.

    good luck.

    162.on 11 Sep 2010 at 1:27 pm 136John. S.

    How do you remove all even integers out of a vector. Lets Sayvec=[45 8 2 6 98 55 45 -48 75]

    163.on 13 Sep 2010 at 10:14 am 137Zane Montgomery

    John. S.,

    Try this trick:

    vec(~mod(vec,2))=[]

    I encourage you to figure out what that does!

    Best,Zane

    164.on 18 Sep 2010 at 5:30 pm 138rudra

    how is it that as we run our for loop, the values of an array are appended? lets say wehave a nested function, how do we create arrays as the for loop proceeds?

    165.on 20 Sep 2010 at 12:19 pm 139Zane Montgomery

    I think I know what youre talking about Rudra.

    If you want to start from a blank array, then fill it with a new value every iteration trythis:

    xx=[] %preallocate your answer vector 'xx'

    %for loop code hereforyy=42 %this is your calculated value that changes every iterationxx=[xx yy]; %this is the key code here that appends the new value tothe end of the old xx vector

    end %end of for loop

    good luck,Zane

    166.on 21 Sep 2010 at 10:28 am 140Jason

    Greetings all:Imagine I have two 2-dim arrays:

    xs = [59, 24.5, 25.5, 26.5, 4;1727, 21.5, 22.5, 23.5, 9;1840, 21.5, 22.5, 23.5, 9;2252, 22.0, 23.0, 24.0, 4;

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    38/53

    2445, 22.0, 23.0, 24.0, 4]

    [x11, x12, x13, x14, x15;x21, x22, x23, x24, x25;

    x31, x32, x33, x34, x35;x41, x42, x43, x44, x45;x51, x52, x53, x54, x55]

    ys= [159, 124.5, 125.5, 126.5;1227, 121.5, 122.5, 123.5;1340, 121.5, 122.5, 123.5;1452, 122.0, 123.0, 124.0;2945, 122.0, 123.0, 124.0]

    [y11, y12, y13, y14, y15;y21, y22, y23, y24, y25;y31, y32, y33, y34, y35;y41, y42, y43, y44, y45;y51, y52, y53, y54, y55]

    Assume the first column of each matrix is a timestamp which can be compared to eachother (e.g. TS0 < TS1 == True). Im using the minutes and seconds only for sake ofbrevety (e.g. 59 is 59 seconds after the hour/minute; 1727 is 17 minutes 27 seconds afterthe hour, etc)

    The return value must be a third matrix, nm, where the first timestamp column willsurvive and the remaining columns will be joined in one of two ways:

    1. First way is to join the arrays if either element has changed.

    So the result should look like this:

    nm = [159, 24.5, 25.5, 26.5, 4, 124.5, 125.5, 126.5;1227, 24.5, 25.5, 26.5, 4, 121.5, 122.5, 123.5;1452, 24.5, 25.5, 26.5, 4, 122.0, 123.0, 124.0;1727, 21.5, 22.5, 23.5, 9, 122.0, 123.0, 124.0;2252, 22.0, 23.0, 24.0, 4, 122.0, 123.0, 124.0;

    ][y11, x12, x13, x14, x15, y12, y13, y14; % element in y changes; x does noty21, x12, x13, x14, x15, y22, y23, y24; % element in y changes; x does noty41, x12, x13, x14, x15, y42, y43, y44; % element in y changes; x does notx21, x22, x23, x24, x25, y42, y43, y44; % element in y does not change; x doesx41, x42, x43, x44, x45, y42, y43, y44; % element in y does not change; x does]

  • 8/4/2019 Matlab Triks

    39/53

    2. Second way is to join the arrays if both elements have changed.

    So the result should look like this:

    nm = [159, 24.5, 25.5, 26.5, 4, 124.5, 125.5;1727, 21.5, 22.5, 23.5, 9, 122.0, 123.0

    ]

    [y11, x12, x13, x14, x15, y12, y13, y14x21, x22, x23, x24, x25, y42, y43, y44]

    Ive made far too many attempts to post code here, but essentially Ive been able to returna matrix to (kind of) match case 1:

    [159 124.5 125.5 126.5;1227 121.5 122.5 123.5;

    1452 122.0 123.0 124.0;1727 21.5 22.5 23.5 9;2252 22.0 23.0 24.0 4]

    but this is not complete of course because it does not actually join the arrays.

    Im new in MATLAB and appeal to the community for help!

    167.on 21 Sep 2010 at 3:27 pm 141Zane Montgomery

    Jason, im trying to go through the logic here but its a bit confusing not knowing theproblem or the same level of detail you do. Im not sure how youre comparing yourmatrices or how you get the desired matrix value you want (one confusion is your ys

    matrix is 54, but your variable ys matrix is 55 while all other inputs are 55 too; areyou missing a column in ys?). It looks like you need some help creating the logicals ofthe comparator and then apply that to concatenating 2 matrices in different waysdepending on the logical value.

    Summary. Im not sure how your values are changed in the following sentences:1. First way is to join the arrays if either element has changed.2. Second way is to join the arrays if both elements have changed.

    Could you give an example using 33 matrix perhaps?

    ttys.

    168.on 22 Sep 2010 at 5:16 am 142Jason

    Thanks for the response this is a bit difficult for me to explain, but here goes:

    The arrays represent price series for derivative (credit default swaps) securities. The goalof the project is to use the joined matrix to run robust regressions (OLS rejection, winsor,ORD, etc.).

    The securities trade infrequenty without a closing price like stocks so we cannotcompare them arbitrarily by lining them in up in series. So the two methods I outlined areways to join the series in ways suitable for regression analysis.

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    40/53

    For sake of argument, use the first two columns of the matricies column 1 representinga timestamp and column 2 representing the price.

    X starts at price 24.5 at time 59 (first row) and does not change until it changes to price21.5 at time 1727 (second row).

    In the meantime, price Y changes from 124.5 at time 159 (first row) to 121.5 at time 1227

    (second row).

    So at this point, nm should have two rows:

    159, 24.5, 25.5, 26.5, 4, 124.5, 125.5, 126.5;1227, 24.5, 25.5, 26.5, 4, 121.5, 122.5, 123.5;

    Column 1 is the timestamp from matrix y.Columns 2 5 are the values from matrix x (note the value is the same in both rowsbecause the value did not change between time 159 and 1227).Columns 6 8 are the values from matrix y (note the values differ in each row becausethe y values change at time 159 and 1227).

    Y does not change again until time 1452 to price 122.

    So Y changes three total times.

    While Y is changing from time 159, x is not changing.

    So if we were to put time times in order (column 1 of both matricies), the times would be:

    59 from x < the x value from this timestamp persists at each y value change below until atleast the next x change at time 1840159 from y1227 from y1340 from y1452 from y1840 from x

    2252 from x2445 from x2945 from y

    The only time we record a price is when one or the other change from the previous value.

    169.on 22 Sep 2010 at 7:58 am 143Zane Montgomery

    Ill check that out and edit this post if/when i get a chance, but in the meantime, you canpost on the MATLAB Central (http://www.mathworks.com/matlabcentral/newsreader/)forums where magnitudes more people will view your question

    170.on 24 Sep 2010 at 8:27 am 144Jason

    Ha ha I did and got no response Ive come to a workable solution altering the programelsewhere, thanks for the response

    171.on 25 Sep 2010 at 6:03 am 145Fess I.

    172.Hi,173.I have been trying to selectively pick a row from a matrix dimension

    of 4,5 using the index. Here's my code.174.175.d = rand(4,5); %randomly generate 4-by-5 matrix176.indx = round((size(d)-1)*rand(1,1)+); % finding indices of the matrix

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.mathworks.com/matlabcentral/newsreader/http://www.blinkdagger.com/http://www.mathworks.com/matlabcentral/newsreader/
  • 8/4/2019 Matlab Triks

    41/53

    177.178.% Here am trying to randomly obtain any row from the matrix179.for i = 1:size(d)180. indxr = d(indx(i),:);181.end182.But, I got this error message: ??? Attempted to access d(5,:); index

    out of bounds because size(d)=[4,5].183.184.Then, I used this for loop below, but it does obtain a row from the

    matrix, but it is same row every time I run it. I wanted to be able toobtain different row on each iteration.

    185.186.for i = 1:size(d)187. indxr = d(indx(i),:);188.end189.190.Do you have any solution or idea on how I can get it to work. The

    basic idea is to randomly select any row from the matrix

    191.on 27 Sep 2010 at 2:57 am 146Shaps

    hi..This is SHAPS..I hav a array of strings stored in a variable A within a if loop.. this variable is gettingoverwritten everytime.. I want it to be stored seperately.. I mean first time when it entersthe if loop it shd be stored in A0 next tym when it enters it shd be in A1 and so on.. howcan i do it?can you pls help..

    192.on 27 Sep 2010 at 2:59 am 147Shaps

    hi.. this is shaps.. i hav a array of strings stored in a variable A within a if loop.. thisvariable is getting overwritten evytime.. i want the value of A to be stored during eachiteration of if loop in the form of A0,A1,A2.. i mean A0 should hav first set of data thenA2 next set of data.. how can i do it.. can you pls help..

    193.on 27 Sep 2010 at 3:44 am 148Shaps

    hi..This is shaps..I hav a array of strings stored in a variable A within a if loop.. this variable is gettingoverwritten everytime.. I want it to be stored seperately.. Like during the first iteration thevalues shd be stored in A0 and next iteration it should be in A1 and so on.. How can i doit? can you pls help me..

    194.on 29 Sep 2010 at 5:56 am 149Mukil

    Hi,I have a doubt.How to replace alternate elements of a row vector with some value. Please help me.for eg [1 2 3 4] should become [1 0 3 0]

    Thank you.Mukil

    195.on 06 Oct 2010 at 9:42 am 150Trey

    You are awesome.

  • 8/4/2019 Matlab Triks

    42/53

    196.on 26 Oct 2010 at 1:51 am 151Christopher Alain Jones

    great post all round!

    very helpful especially the interesting debates

    197.on 08 Nov 2010 at 1:06 pm 152Jacob

    I need to make the matrix:

    [1 3 5 7; 2 6 10 14; 3 9 15 21]

    Is there a shortcut for this?

    198.on 18 Nov 2010 at 12:18 pm 153luke

    Jacob,

    Your matrix is composed of multiples of first row (or column). Suppose that the firstcolumn is c (given), then your matrix is:

    A=c*(1:numel(c));

    If first row is given (say r), then:A=(1:numel(r)).'*r;

    199.on 13 Dec 2010 at 10:30 am 154basu

    how to display the maximum and minimum group elements in a duplicate vector usingmatlab code

    if the element present in the max group display corresponding max group elementsandif the element present in the min group display corresponding min group elementshow?Help me

    200.on 14 Dec 2010 at 2:41 pm 155Kajal

    Hello All,I have an algorithm that finds out the best features in a matrix and gives out the output asthe indices of the input data that were selected as best features.

    The input is a 16*1368 matrix and the output is a 500*1 matrix.

    I want to compare the indices obtained as the output to the input indices and if they matchI want to copy the rest of the row in a separate matrix.

    For example:

    InputCol 1 2 3 4A=[ 100 20 3 4 5 6 7 8---->row19 10 11 12 13 14 15 16----->row 224 67 21 38 42 53 54 90]> row 3

    Output: [1 2]> column indices

    Now I want a matrix C such that C= [100 9 2420 10 67]

    Please help me out !

    http://mathwork/http://mathwork/http://mathwork/
  • 8/4/2019 Matlab Triks

    43/53

    Thanks and regards,Kajal

    201.on 22 Dec 2010 at 10:32 am 156Zane Montgomery

    Kajal,

    Im going to assume you only need help making matrix C and you know how to buildyour input/output matrices?

    In the code, I have some A (input) and B (output) matrices already populated whichhopefully you can do already. B contains the arbitrary number of indices in A you wantto put into C.

    A=[ 100 20 3 4 5 6 7 8; 9:16; 24 67 21 38 42 53 54 90];B=[1 2];

    C=[]; %generate C as an empty matrix

    for n=1:length(B)C=[C; A(:,B(n))']; %Add a new row to C using the column of A

    defined by index Bend

    C %prints out C

    Should work! good luck.-Zane

    202.on 12 Jan 2011 at 1:37 pm 157Wolfhantich

    hello everybody, I have to do this I need to add value 3 to all odd values of vector x.how can I do that?

    thx

    203.on 20 Jan 2011 at 12:49 pm 158iaj4

    Hi all, I do have a vector with real data that was colected every 5 minutes. i want to havea matrix with data every one minute. initial matrix A=[1 2 3]; what I want is B=[1 1 1 1 12 2 2 2 2 3 3 3 3 3].Any advice on how to do it?thanks!

    204.on 22 Jan 2011 at 6:26 am 159jay

    HiI have a vector with both +ve and -ve values and the index vector. Both are taken from alarge vector. While sorting the vector again according to -ve to positive I would like tokeep track of the index also and arrange it accordingly..Please let me know

    thanks

    205.on 24 Jan 2011 at 7:24 am 160Leandro

    HiI have two vectors. For example:x1 = [0.6154 0.7919 0.9218 0.7382 0.1763];x2 = [0.4057 0.9355 0.9169 0.4103 0.8936];How I can do

    http://www.blinkdagger.com/http://www.blinkdagger.com/http://www.blinkdagger.com/
  • 8/4/2019 Matlab Triks

    44/53

    x3 = [0.6154 0.4057 0.7919 0.9355 0.9218 0.9169 0.7382 0.4103 0.1763 0.8936]? I dontwant to use forThanks.

    206.on 03 Feb 2011 at 9:05 am 161Felix

    hi,

    i have a 3D Matrix such as

    QQ=cat(3,w,q)

    QQ(:,:,1) =

    1 23 4

    QQ(:,:,2) =

    5 67 8now i want to use

    [r c]=find(QQ,8)to find my position for point 8. find just works for 2D arrays . is there is possibility for3D arrays?

    207.on 05 Feb 2011 at 11:01 am 162Anonymous

    @anuj on 12 Mar 2008 at 3:06 am 10

    QUESTION

    How can I remove rows in a matrix that have third element in the column 0

    Input-1 2 3

    2 3 00 2 30 2 0

    Answer should be-1 2 30 2 3

    ANSWER

    >> c=[1 2 3;2 3 0;0 2 3;0 2 0]

    c =

    1 2 32 3 00 2 30 2 0

    >> z=find(c(:,3)==0)

    z =

  • 8/4/2019 Matlab Triks

    45/53

    24

    >> c(z',:)=[]

    c =

    1 2 30 2 3

    Answered this question by reading this page alone.. Thanks for the post..New Comer to this page.

    208.on 05 Feb 2011 at 11:02 am 163Anonymous

    >> c=[1 2 3;2 3 0;0 2 3;0 2 0]

    c =

    1 2 32 3 00 2 30 2 0

    >> z=find(c(:,3)==0)

    z =

    24

    >> c(z,:)=[]

    c =

    1 2 30 2 3

    209.on 06 Feb 2011 at 1:37 pm 164vijay

    hi,I am trying to implement linear convolution by my own, as a part of class project.

    my query islet

    a=[1 2 3; 4 5 6;7 8 9]

    I wish to display only the second row. i.e 4 5 6.

    So, what command should i use?

    210.on 06 Feb 2011 at 1:39 pm 165vijay

    hi,I am trying to implement linear convolution by my own, as a part of class project.

    my query islet

    a=[1 2 3; 4 5 6;7 8 9]

  • 8/4/2019 Matlab Triks

    46/53

    a(6), displays the element of 6, but I would like to know , how to make it display theentire row/column .I wish to display only the second row. i.e 4 5 6.

    So, what command should i use?

    211.on 07 Feb 2011 at 8:21 pm 166vijay

    the command to be used is a(2,:)212.on 08 Feb 2011 at 11:03 am 167Ted

    Ive been trying to delete all the columns of a HUGE matrix that contains all zeros:

    info:my matrix size is (60,4000000) which is pre-allocated

    example:i have data on the 2500000 and belowafter the 2500000th column i only have zeros.

    question:how do i get rid of all the columns from 2500000 4000000 without getting the out ofmemory error.

    tried:matirx(:,2500001:end)=[];newmatrix=matrix(:,1:2500000);

    i cant even copy the old matrix with a new one without getting the out of memoryerror. Please help, thanks!

    213.on 21 Feb 2011 at 2:34 am 168chitra

    hi,i need a matlab code which can store pixel values in a register or a 1D array.andi need code for converting m*m matrix to m*1 row matrix

    214.on 22 Feb 2011 at 7:57 am 169Colm

    how do i change all the 0s in a matrix to the value 1.thxcolm

    215.on 25 Feb 2011 at 5:23 am 170Ashutosh Gupta

    Hi All,

    i have a matrixA=[70, 71, 72, 73, 73, 74, 75, 75, 75, 75, 76, 76, 76, 77, 77, 77, 78, 78, 78, 78, 79, 79, 79,79, 79, 79, 79, 80, 80, 80, 80, 81, 8181, 81, 81, 81, 81]

    i have to find the frequency of repeated elements as well as distinct element list likeb = 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81 // array of distinct elementsfreq = 1, 1, 1, 2, 1, 4, 3, 3, 4, 7, 4, 7 // frequency of each distinct element

    please help me to do this task

    216.on 27 Feb 2011 at 4:11 pm 171mansur

    http://www.cirdanimaging.com/http://www.cirdanimaging.com/http://www.cirdanimaging.com/
  • 8/4/2019 Matlab Triks

    47/53

    Dear all,do you have any idea how could I accumulate every three member of an array and save itt o another array:

    Lets assume:

    A=[1;2;3;4;5;6;7;8;9]

    I would like to have this matrix:

    B=[1+2+3;4+5+6;7+8+9]

    Thanks

    217.on 28 Feb 2011 at 2:31 am 172mansur

    The answer:B= kron(speye(3),ones(1,3))*A;

    218.on 01 Mar 2011 at 3:17 pm 173Liz

    Hi there!Is there a function that would enable me to find the proportion of values in an array thatare above or below a certain value?Thanks, Liz

    219.on 15 Mar 2011 at 1:23 am 174Mar

    Hi all,

    I got this problem:

    How do I repack the one column vector(with 200 elements) into 50 column vectors with4 elements each? Also, I need to have those new vectors as a part of one matrix.

    Thank you.

    220.on 17 Mar 2011 at 5:04 am 175kyrzi

    dear all,i want to make a 66 matrix with 0s and 1s.but i want a specific number of 0s (and 1s).for example, all arrays must have 4 zeros and 2 ones.Any help would be greatly appreciated.

    221.on 06 Apr 2011 at 3:57 pm176Guy

    Hello,

    Very useful tutorial, it helped me a lot to start with Matlab.

    Thanks.

    222.on 07 Apr 2011 at 5:26 pm177Priyabrata GhoraiHi,

    Im havin

    a=10101101i want the output asb=[1;0;1;0;1;1;0;1]

    http://opt/scribd/conversion/tmp/scratch7027/http://-http://opt/scribd/conversion/tmp/scratch7027/http://-http://opt/scribd/conversion/tmp/scratch7027/http://-
  • 8/4/2019 Matlab Triks

    48/53

    how to do it in matlab? Also vice versa when a=[1;0;1;0;1;1;0;1] and its output asb=10101101

    I want each bit to fit in each cell of the matrix and vice versa.

    223.on 07 Apr 2011 at 6:30 pm178Parul

    Dear all,I have a vector 2Dx= (1 1 2 1 2 2 1 2 3 2;5 6 7 8 9 2 3 4 5 6)I need to separate for1 corresponding end columnI mean first column 1 : 5 6 8 3first column 2: 7 9 2 4 6how can I separate the for each duplicate the corresponding elemts of the second columnplease

    Thanks in advancedparul

    224.on 01 May 2011 at 8:21 am 179Reza

    thank you all. It was really helpful for me as a beginner.

    225.on 01 May 2011 at 9:05 pm 180sam

    hi friends,I have a problem. I have two binary arrays for example,a=[101011;100011;110101];b=[10; 01;11];I would like to concatenate these two binary arrays as

    output=[10101110;10001101;11010111]. Please help me how to get this putput?output[a b] is not working.

    Thanks

    sam

    226.on 06 May 2011 at 9:13 pm 181mona

    227.%after constrained delaunay triangulation ,centroids plotted in eachtriangle

    228.%I want to join with line segment two adjacent triangles229.% but nor plot or line function works correctly230.% i am getting error unequal vectors

    231.tr = TriRep(dt(io, , dt.X);232.triplot(tr);

    233.axis ij;234.axis([40 140 40 140]);235.ic = incenters(tr);236.hold on;237.plot(ic(:,1),ic(:,2),'*r');238.hold off;239.n=size(tr);240.ind1 = tr(1,:);241.ind2 = tr(2,:);242.tri1 = dt.X(ind1,:);

  • 8/4/2019 Matlab Triks

    49/53

    243.tri2 = dt.X(ind2,:);244. if((isequal(tri1(1,1),tri2(1,1)) && (isequal(tri1(1,2),tri2(1,2))))||

    (isequal(tri1(2,1),tri2(2,1))&& (isequal(tri1(2,2),tri2(2,2))))||(isequal(tri1(3,1),tri2(3,1))&& (isequal(tri1(3,2),tri2(3,2)))));

    245. c1=incenters(tr,1);246. c2=incenters(tr,2);

    line(c1,c2,'Marker','.','LineStyle','-');

    247.on 09 May 2011 at 10:14 pm 182dj62

    hey, I was wondering how would I change a dec to binary then storing each bit of thebinary into array

    I would first use a= dec2bin(255,8), Then from here im stuck.. the output will be11111111. How do I this number in a 18 matrix?

    thank you

    248.on 09 May 2011 at 10:18 pm 183dj62

    D= figured it out xD..

    Noticed that it is a char. So just use str2num and store =)249.on 12 May 2011 at 8:00 am 184marwa

    Sam, you can try [a b]

    250.on 12 May 2011 at 8:07 am 185marwa

    Sam,[a;b] or [a,b] depends if you want to do it by rows or columns

    251.on 12 May 2011 at 8:14 am 186marwa

    Colm:If A matrix contains zeros,B = A==0will replace all entries in A to 1

    252.on 13 May 2011 at 11:27 am 187Florin

    hello everyone, i kinda have a big problem, i have to check if two images overlap, but idon`t know how to get the indices to check each other out. Let me explain it better:p and t are my images(they are white with black lines -1 pixel width- )i`m using

    bwP = bwlabeln(p,4);bwT = bwlabeln(t,4);

    to separate the areas into groups(delimited by the black lines).

    My problem is that i now have to check how many pixels from t (including whichgroup they belong to) overlap with the pixels from a group in p (basically i have tocheck each region in the image P and see how many pixels from T(and the regions thatthey belong to) overlap.

    i`m not that familiar with matlab so any help would be greatly appreciated.

    253.on 02 Jun 2011 at 7:19 pm 188Pabitra

    Hi Friends,I cant solve this prov pls help me

  • 8/4/2019 Matlab Triks

    50/53

    P=[1,5,2,4,6,2,5,2,4,2,5,2,3,2,5,2] this is an array ..i want to find out an array which contains the repeated value.Ans: Q=[2,2,2,2,2,2,2,4,4,5,5,5,5]

    254.on 05 Jul 2011 at 1:01 am 189manju

    hello

    i have a matrix .this is a addition of two sorted vector.i need a access a element which is two place in a matrix. when i trying to find a elementin a matrix ,it returns two row and column value.then how to access first from two sameelement..

    Thanks in advance

    manju

    255.on 14 Jul 2011 at 11:13 am 190Keana

    Hi everyone, Ive been trying to create an array whose elements consist of a contant(say100) throughout but with size equal to the size of another array. Please can someonehelp me with a way out.

    256.on 16 Jul 2011 at 2:09 pm 191ameen

    hi Keana,We suppose G have size m,n and we want to create G1 have the same size of G and allelements egal to 100

    [m,n]=size(G);G1=ones(m,n)*100;

    257.on 22 Jul 2011 at 12:53 am 192Jesse Vaitkus

    Hello, Id just like to say that this was highly informative and I have been searching forsuch a bare basics guide for a long time. Thank you very much.

    Cheers,Jesse Vaitkus

    258.on 25 Jul 2011 at 8:41 am 193bZETsu

    hi i nid help. I generated a column array of numbers like this2468101214

    1618And n0w i want it to be an array just lyk ds2 4 68 10 1214 16 18How wud i do it? Help plz thanks

    259.on 26 Jul 2011 at 3:14 pm 194r1

  • 8/4/2019 Matlab Triks

    51/53

    Hi All:I have a matrix as follows:A =[1.000000000000e+00 1.062210069028e-04 7.122271145085e-06-1.000000000000e+00 2.141751975226e-05 1.070956793608e-041.000000000000e+00 2.141751975226e-05 -1.070956793608e-042.000000000000e+00 4.883168521903e-05 2.801214863122e-05-2.000000000000e+00 -6.892929607953e-05 1.128055154927e-043.000000000000e+00 -1.333947488675e-05 -2.893511554255e-04-3.000000000000e+00 -6.060199942383e-05 -1.315659482030e-043.000000000000e+00 -6.060199942383e-05 1.315659482030e-04-3.000000000000e+00 1.333947488675e-05 -2.893511554255e-04]I want to seperate them into three matrices like :A1 = [1.000000000000e+00 1.062210069028e-04 7.122271145085e-06-1.000000000000e+00 2.141751975226e-05 1.070956793608e-041.000000000000e+00 2.141751975226e-05 -1.070956793608e-04]A2 = [2.000000000000e+00 4.883168521903e-05 2.801214863122e-05

    -2.000000000000e+00 -6.892929607953e-05 1.128055154927e-04]A3 = [ 3.000000000000e+00 -1.333947488675e-05 -2.893511554255e-04-3.000000000000e+00 -6.060199942383e-05 -1.315659482030e-043.000000000000e+00 -6.060199942383e-05 1.315659482030e-04-3.000000000000e+00 1.333947488675e-05 -2.893511554255e-04]

    Waht is the efficient way of doing this?Please not that the elements in the first colomn are not equally distributed and hence, Icannot extract by specifying the certain length.I would really appreciate your help.R.

    260.on 31 Jul 2011 at 9:57 pm 195art

    HelloI have an integer collection. I need to get all possibilites that sum of values are equal toX.

    261.on 14 Aug 2011 at 3:01 am 196Pink

    Hey can u please tell me how can we find the number of repeated 1s(if its repeated only3 times not more then dat) in a given matrix nd a vector???

    for eg: A=[ 1 2 3 1 1 1 0 0 0 1 1 0]should give ans as 3

    Leave a Reply

    Include MATLAB code in your comment by doing the following:

    %insert code here

  • 8/4/2019 Matlab Triks

    52/53

    Top of Form

    Name (required)

    Mail (hidden) (required)

    Website

    Bottom of Form

    Like the content? Donate $5 to keep this site up!

    Top of Form

    Bottom of Form

    Links The MathWorks Blog Ring

    http://blogs.mathworks.com/http://www.9rules.com/http://feeds.feedburner.com/blinkdaggerMatlabhttp://feeds.feedburner.com/Blinkdaggerhttp://blogs.mathworks.com/
  • 8/4/2019 Matlab Triks

    53/53

    Undocumented MATLAB Tips

    Data Mining in MATLAB

    Top of Form

    Search for:

    Bottom of Form

    Categories blog (63)

    Computer Games (6)

    Control Systems (9)

    Data Processing (7)

    Flash (1) GUI (33)

    MathWorks Interview (7)

    MATLAB (93)

    monday math madness (45)

    MySQL (3)

    PHP (5)

    statistics (6)

    Uncategorized (1)Copyright 2011 blinkdaggerPosts RSSComments RSS

    WordPress Themedesigned byDavid UlianaXHTMLCSS

    http://undocumentedmatlab.com/http://matlabdatamining.blogspot.com/http://blinkdagger.com/category/blog/http://blinkdagger.com/category/games/http://blinkdagger.com/category/matlab/control-systems/http://blinkdagger.com/category/matlab/data-processing/http://blinkdagger.com/category/flash/http://blinkdagger.com/category/matlab/gui-matlab/http://blinkdagger.com/category/mathworks-interview/http://blinkdagger.com/category/matlab/http://blinkdagger.com/category/monday-math-madness/http://blinkdagger.com/category/mysql/http://blinkdagger.com/category/php/http://blinkdagger.com/category/matlab/statistics/http://blinkdagger.com/category/uncategorized/http://blinkdagger.com/feed/http://blinkdagger.com/comments/feed/http://wpthemepark.com/themes/intense/http://saltedsugar.com/http://validator.w3.org/check/refererhttp://jigsaw.w3.org/css-validator/check/refererhttp://jigsaw.w3.org/css-validator/check/refererhttp://undocumentedmatlab.com/http://matlabdatamining.blogspot.com/http://blinkdagger.com/category/blog/http://blinkdagger.com/category/games/http://blinkdagger.com/category/matlab/control-systems/http://blinkdagger.com/category/matlab/data-processing/http://blinkdagger.com/category/flash/http://blinkdagger.com/category/matlab/gui-matlab/http://blinkdagger.com/category/mathworks-interview/http://blinkdagger.com/category/matlab/http://blinkdagger.com/category/monday-math-madness/http://blinkdagger.com/category/mysql/http://blinkdagger.com/category/php/http://blinkdagger.com/category/matlab/statistics/http://blinkdagger.com/category/uncategorized/http://blinkdagger.com/feed/http://blinkdagger.com/comments/feed/http://wpthemepark.com/themes/intense/http://saltedsugar.com/http://validator.w3.org/check/refererhttp://jigsaw.w3.org/css-validator/check/referer