Escolar Documentos
Profissional Documentos
Cultura Documentos
blinkdagger
an Engineering and MATLAB blog
Home
Listchecker
MATLAB
Contact
About
Introduction
1-D Arrays (also known as vectors) are commonly used within Matlab, so it is a good idea to understand how
they work and how to bend them to your will. This is a quick tutorial on some simple tricks that you may or may
not know about vectors.
Creating Vectors
1. How to create a row vector that increments by 1. For example, let’s create a row vector that goes from 1 to 10, with
increments of 1.
myVector = [1 2 3 4 5 6 7 8 9 10]; %the hard way
myVector = 1:10 %the easy way
Note: Dan Kominsky pointed out that is a subtle but important difference here. When you are working with real
numbers the difference is irrelevant, but when you are dealing with complex numbers, the meaning is entirely
different! Thanks for the correction, Dan.
3. How to create a column vector that increments by 1. For example, let’s create a column vector that goes from 1 to 10,
with increments of 1.
myVector = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]; %the hard way
myVector = [1:10].' %the easy way
4. How to create a vector that increments by a specific value. Let’s create a vector that goes from 1 to 19, and
increments by 2. Note that the increment value is not limited to integers.
myVector = 1:2:19
5. How to create a vector that decrements by a specific value. Let’s create a vector that goes from 10 to 1, and
decrements by 1
myVector = 10:-1:1
6. How to create a vector with equally spaced points. Let’s create a vector that goes from 0 to 100 with 21 equally
spaced points.
%first argument is the start value of the vector
1 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
7. How to create a vector of zeros. For example, let’s create a vector of 10 zeros.
%first argument is the number of rows
%second argument is the number of columns
rowZeros = zeros(1,10)
Note: Incidentally, this is a great way to preallocate a vector. Preallocating a vector is most useful when FOR loops are
involved. Preallocating a vector is preferred over resizing a vector repeatedly as it reduces the processing time.
8. How to create a vector of ones. For example, let’s create a vector of 10 ones.
%first argument is the number of rows
%second argument is the number of columns
rowOnes = ones(1,10)
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 done repeatedly, there is a speed penalty.
11. How to remove a particular element from a vector. Lets say we want to remove the 4th entry.
myVector = 1:10;
myVector(4) = []
12. How to replace a particular element with a different element within a vector. Lets say we want to replace the 4th entry
with the value of 100.
myVector = 1:10;
myVector(4) = 100
15. How to keep the last 5 elements (or equivalently, remove the first five elements).
2 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
myVector = 1:10;
myVector = myVector(end-4:end)
16. How to remove a series of elements. For example, let’s remove entries 3 through 6:
myVector = 1:10;
myVector(3:6) = []
17. How to keep a series of elements. For example, let’s keep entries 3 through 6:
myVector = 1:10;
myVector = myVector(3:6)
18. How to remove a group of specific elements. For example, lets remove entries 2,5, and 7:
myVector = 1:10;
myVector([2,5,7]) = []
19. How to keep a group of specific elements. For example, lets keep entries 2,5, and 7:
myVector = 1:10;
myVector = myVector([2,5,7])
20. How to get the number of elements within a vector. Useful when creating a for loop to run through a vector.
myVector = 1:10;
numElements = length(myVector)
%index contains the indices elements within myVector which are non-zero
index = find(myVector);
myVector = myVector(index) %removes all the zeros within the vector
22. How to remove a particular value from a vector. For example, how to remove any occurence of 6 within a vector
myVector = [6 6 0 1 2 3 0 0 6 6 1 2 0 0];
%index contains the indices of elements within myVector which are equal to 6
index = find(myVector == 6 );
myVector(index) = []
%index contains indices of elements within myVector which are greater than 5
3 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
25. Similarly, how to remove all elements less than 5 from a vector.
myVector = [10 0 0 1 12 3 0 0 4 5 1 12 0 0];
%index contains the indices of elements within myVector which are less than 5
index = find(myVector < 5);
myVector(index) = []
32. How to get the product of all the elements within a vector.
myVector = 1:10;
total = prod(myVector);
33. How to get the average, standard deviation, and variance of a vector.
myVector = 1:10;
averageArray = mean(myVector)
stdArray = std(myVector)
varArray = var(myVector)
4 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
175 Responses to “Matlab - Tips and Tricks on Manipulating 1-D Arrays (Vectors)”
1. on 14 Jan 2008 at 7:46 am 1Dan K
I’m 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 working with real numbers the difference is irrelevant, but the
myVector’ is the complex conjugate 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
Dan K:
Thanks for the correction. It is a subtle, but important point!! I have edited the post to reflect your comment.
Thanks,
Quan
Actually, Quan, there is one other major area in which your code is inefficient (although functionally correct). It is
generally significantly faster to use logical indexing rather than the find command. For example in item number 24 you
use:
index = find(myVector > 5);
myVector(index) = []
myVector(myVector >5)=[];
HTH,
Dan
Thanks Again Dan. Your suggestion is duly noted and I have edited the post to reflect it.
I need to muliple a vector to every single column (as the same size of the vector) of a matrix element by element
without using the for loop. Is there any way to do it?
myVectorRepeated = repmat(myVector,1,4);
5 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
I visited your website and i found it very useful. Dan comments were even better. Great work!
I’ve 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.
Percy,
Try this:
a(:,indicesToRemove) = [];
a(indicesToRemove,:) = [];
My question is -
How can I remove rows in a matrix that have third element in the column 0
Input-
123
230
023
020
assuming that I just have the vectors of a and b, but doesn’t know the index of b in a.
setdiff(a,b,’rows’)
6 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
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
>pp
[1] 0.80 3.00 0.30 0.00 0.20 1.30 9.10 0.09
Takkari,
Quan
Hi Quan,
sort(y) just sort the data.
What I need is a vector containing indices of the sorted values in original vector y.
Takkari,
You could have said that in the first place, Instead of showing us the R code.
[y,i] = sort(y)
p=[p find(y==x(i))];
end
length of x is 392480
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:n
h(t)=omegac+alphac*Y(t-1)+betac*h(t-1);
b(t)=Y(t-1);
7 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
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);
end
end
Thanks
Takkari,
Quan
h(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:n
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);
end
end
Hey! I just wanted to say that your MATLAB tutorials are great, I found them extremely helpful. Everyone else’s
comments were helpful as well. You have a great website!
Thanks
n=1:10;
average=mean(n)
I recently found the unique(V) function very useful in a permutations algorithm which generated the correct answer
several ways and stored them all. To prune it down, I used unique.
8 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
I have a matrix and I want to remove all columns that are zeros. Does anyone know how to do that?
Thanks!
Hey All,
example
I got it
We can use
Thanks a lot
A = [1 2 3; 4 5 6];
% if i want to add all the values greater than 3, then
a(a<3)=0;
t=cumsum(cumsum(a),2);
t(end)
9 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
if i have to know the 2nd order polynomial fit coefficients(c) and respective errors(e) for
I used command
[c e]=polyfit(x,y,2)
e = R: [3x3 double]
df: 3
normr: 0.0669
Thank you
madhu
Madhu,
Have you tried using the cftool command? That might do the trick
Quan
Good stuff here. I was hoping if someone could help me with this problem that I need to get around in an efficient way
(i.e I have to repeat this over many row vectors). Say, I’ve got 3 vectors :
A = [1 2 3 4];
B = [5 6 7 8];
C = [9 10 11];
(note the unequal dims). And I’d like to have a code that gives the following result:
Result = [1; 5; 9; 2; 6; 10; 3; 7; 11; 4; 8];
Hi Baba,
Here is my idea:
%first, pad all vectors using NaN so that they are the same length
C = [9 10 11 NaN];
10 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Result(isnan(Result)) = [];
Quan
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 which are greater than it, how can I just get the first number
which is greater than the specified value.
Thanks.
Adi
Hi asur,
u can use
for i=1:numel(vector)
if vector(i) > 90
index=i;
break
end
end
Hi,
I wonder if there is a built in function that would give me the number of times ‘k’ appears in a vector ? E.g. vec = [1 1
1 2 2 3 3 4 ] where k == 1
ans = 3
Hi,
I wonder if there is a built in function that would give me the number of times ‘k’ appears in a vector without using the
hist function. ? E.g. vec = [1 1 1 2 2 3 3 4 ] where k == 1
ans = 3
Thank you very much for your help and for your time!!! thank you very much!!!
Hi asur,
11 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
find(vector >90,1)
to get the index of the first number greater than 90 in the vector.
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)==1
soln=soln+1;
else
end
end
@Graham, Zane,
…OR, you could shoot the “vectorizer” at this problem. Something like:
vec = [1 1 1 2 2 3 3 4 ] ;
sum(vec==1)
Hope this helps! Can’t find it right now, but there is a great post by Loren about indexing that I think covers logicals
like this. Also skim Steve’s blog for logical operations since they’re used in image processing a lot.
Merry Christmas!
Rob
@ Tanuj,
Since I’m on a roll with vectorizing and using logical indexing, I see you had an interesting problem farther up the
page. If you need to sum up all the elements of A that are 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 are greater than 3″
I don’t want to duplicate what Loren’s and Steve’s blogs have covered about logicals, but this method basically
produces a “mask” vector of 1’s and 0’s the same size as A. It tells the 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 a time, and avoid using loops. Less
mess for you, and usually faster code.
HTH,
Rob
hi
12 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
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 that
a= 1 2 8 9 3 4 5
thanks
@ 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 with
a = 1:5;
inserts = [8 9];
b = zeros(numel(a)+numel(inserts),1);
HTH,
Rob
@fawx
Rob’s method is extremely good for especially large arrays. Pre-assigning the size of arrays is good practice to improve
the processing speed.
% force whatever elements you want between segments of your 1-D array.
b=[a(1:3) inserts a(4:end)]
fawx
Hi Dan,
Great tips !
I am stuck with a problem related to sorting of a vector:
I have following vector
13 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
vector = [1 1 0 0 0 1 1 1 0 1 1 1 0]
Thank you
Rahul
ans = 2,3,3
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 and storing those averages. Here is my
solution:
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 to improve it further if possible.
Do you guys have another suggestion?
Thank you.
Hello everyone, I’m sorry if this has already been answered, but is there no built-in function 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 element intact:
a == [1]
b == [11,111]
Any such operator or do I need to create it myself? I’m not a programming guru so I’m not certain how to create this
functionality in the most efficient way.
Thanks in advance, I’m sure someone has a great answer to a beginner such as myself ^^
14 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Hi Matt,
What you could do is before removing some elements of a(:), store them in another vector 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 of a(:) equal to 11:
a = [1,11,111];
b=a(a==11);
a(a==11)=[];
-Yo
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
101001
101001
101001
101001
.
.
.
(N) times
Our try:
function Y = jail(X,p)
t = 1:1:c;
M = sin((pi*t)/p).^2 ;
F = repmat(M,r,c);
Y = F;
Thanks in advance
@Johan&Jeppe,
I’m curious if the matrix you’re getting is 1-D or 3-D+? I would think it should still output as 2-D, just not the style that
you want.
Let’s call your vector ‘a’. Since you want to make a new matrix that is a column vector of many ‘a’ vectors, your
repmat should be repmat(M,N,1) where N is the N times you wanted above (aka ‘r’ in your code)
15 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
function Y = jail(X,p)
This will stack the matrix/vector on top of itself r times. Hope that helps!
-Zane
Thanks Zane for the quick response, this solved our problem.
How do I return the, say, first element of an array expression without storing the
expression into a temporary variable first? Eg
result = first(expression);
% rather than
tmp = expression;
result = tmp(1);
Hi .. Its really usefull wesite.. I need to know how to get the average of group of vectors…Lets 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??
I wanted to insert the zero column vector in the matrix. I have this matrix
[1 0 0 1
0011
1110
0 1 0 1]
How do I convert it to
[1 0 0 0 0 0 1 0
00001010
10101000
0 0 1 0 0 0 1 0]
Thanks
Hi Quan,
Thanks for all of the tips. I HATED shifting vectors! It’s not practical in most cases. Quan, you should check out the
circshift command… it’s easy and you just enter the ammount that you want the vector shifted (you do however, have
to convert the vector into a colum vector…):
16 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
u = [1:10];
u1 = (circshift(u',3))'
% This shifts vector "u" 3 places to the right, and places the last three elements
% in the first three places!
hey,
i’ve done a blunder by performing a set of operations on the wrong array, due to typing error. 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 by rote? there’s really lengthy stuff preceeding it..
thanks,
nirveda
Now
how would i filled the values of this array to
[1 2 3 4 .....
101 102 103...
201 202 203
]
jose velez:
There might be a better way than the for loop, but this will do the job quite nicely.
Example
array= [1 2;
1 2;]
how do i transform the previous array to look like
array =[1 2 0 0 0;
1 2 0 0 0;]
17 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Keep in mind I cant do it manually it has to be by MATLAB code because is a 31 x 31 array ( lots of numbers)
Thank you for your efforts.
Jose,
ArrayNew=[Array2 zeros(31,10)]
This concatenates a zero array of size 31×10 after you original 31×21 array2. You can play around with that, but it is
very specific on dimensions of each array. This can also be expanded/parametrized for an MxN array –> MxM array.
Good luck!
-Zane
%my array
windspd = [1,2,3;4,5,6;7,8,9]
winddir = [30,60,120;135,180,210;225,250,300]
% code not running yet, i have problem how to calculate in array with 2
% variabel & calculate north-south direction in winddir so i have north-south wind speed component
% north(0), east (90), south(180) & west (270)
% correct me if i'm wrong with the if logic calculation i'm still new in matlab
if winddir (find(winddir>=0 & winddir=90 & winddir=180 & winddir=270 & winddir<
winddir = cos(winddir*(pi/180))
end
Hi Budhie,
Somethings wrong with your ‘if’ statement, but that’s not how I usually code in MATLAB, but let me show you a cool
tip.
It looks like you’re just trying to convert the original winddir vector from degrees to radians and then taking the N/S
(cosine) component of it?
18 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
WinddirRad=winddir*pi/180;
WinddirNS=cos(WinddirRad);
But even easier, MATLAB has built in capability to handle degree measurements:
WinddirNS=cosd(winddir);
% and you're done!
That got me what you were looking for, hopefully it works for you too.
-Zane
HI Zane,
its works, thanks for the cool tips :D. yes i want to separete the wind component. but i still have problem doing
operation in array.
% i want to do operation between 2 array, but only on wind direction selected value and keep other value
% how i can do that in matlab?
if (b >=90)&(b=180)&(b<270)
a = a*cosd(b-180)
end
end
a = [1,2,2.59807;2.82842,-5,-5.19615;-4.94974,-2.73616,9]
thx be4,
-budhie-
% i want to do operation between 2 array, but only on wind direction selected value and keep other value in array a
that not selected.
% how i can do that in matlab?
if (b >=90)&(b=180)&(b<270)
a = a*cosd(b-180)
end
end
a = [1,2,2.59807;2.82842,-5,-5.19615;-4.94974,-2.73616,9]
19 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
thx be4,
-budhie-
if (b >=90)&(b=180)&(b<270)
a = a*cosd(b-180)
end
end
if (b >=90)&(b=180)&(b<270)
a = a*cosd(b-180)
end
else if (b >=180)&(b<270)
a = a*cosd(b-180)
end
end
thx be4,
-budhie-
Hello,
A = [ 2.1 1.2 2.3 1.3 1.3 3.4 5 2 6 8.4 5.4 5.4 3.4 5 2 4.4 2.2 4.5]
now there appears 3.4, 5 and 2 twice consecutively in the above code, and I see it as a pattern. I need some solution to
extract these values at a certain min threshold to max threshold sequence.
Alternatively I have seen in this very useful blog that values greater than / less than in a vector can be replaced by
some, but is there some solution that a block of values be replaced by some value for e.g. all values greater than 3 and
less than 7 be changed to 0 in a vector.
20 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
/Malaika.
Can i do this without using loop with the help of array indexing or vectorising?
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}
Tim,
I’d recommend creating a new variable b to store you 0s/1s array, but other than that it’s easy.
Good luck,
Zane
hi everyone ,
This website is really useful for us. plz i need a help . How to group an array elements within range. plz help me.
21 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
>> dec2bin(5)
ans =
101
Daithi,
Try dec2bin(x,b) where x is your decimal number and b is the number of bits.
GL,
Zane
I have a array of dimension 5X16 ( 5 rows and 16 columns ) . I want to separate it into 2 matrices having dimensions
5X8 and 5X8 with odd columns in one group and even columns in other group using matlab
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 array
eg: 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]
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 ask which are equal to one and destroy.
krishh,
Here’s an example:
a = [1 2 3 4;5 6 7 8]
b = a(:, 1:2:end) % odd columns
c = a(:, 2:2:end) % even columns
The ‘b’ and ‘c’ equations would remain the same for any size a.
22 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
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]
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]
Hank,
A=[1 3 5]
B=[2 4 6]
C = zeros(1,length(A)*2); % initialize C
C(1:2:end) = A % place A’s contents into C starting at 1, then 3, etc.
C(2:2:end) = B % place B’s contents into C starting at 2, then 4, etc.
And a correction to comment #91 - I obviously removed all the odd numbers instead of the evens as asked.
Change:
a(rem(a,2) == 1) = []
to:
a(rem(a,2) == 0) = []
this will flag the even numbers for removal.
[1 0 2 0
3 0 4 0]
should becom
[1 2
3 4]
sangeeta,
23 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
There is a problem when attempting to “delete” the zeros - or any value - from a matrix. If there is not a square data
set contained within a larger, padded set you will not be able to operate on it efficiently. I think you need to ask
yourself why are you arriving at this position? What operation is forcing you to take such an action? The best solution
would be to come up with a better algorithm that wont leave you with your back against the wall.
Again, the example above is quite symmetric. It would require initializing a new matrix and then filling it in as my posts
above show.
[1 3 8
692
589
7 6 3]
[1 3 8
6 9 2]
[1 8 8
6 6 2]
can u tell me how to convert the text file containing ascii code into binary number and store in array in matlab………..
Hello,
155555
016666
001777
000188
000019
000001
24 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
how I can change the position of one element without changing the size of the matrix.
Thanks
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
hi
to
1111
2222
5555
i tried too many way but couldnt find the right way.
@ali
I’m not quite sure I know what you’re 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 be swapped. It works well for
large segments of unchanged vectors like you see in the last vector element. It could be x(4:500) if those are all
unchanged which saves lots of time.
@waqas
Thanks to a recently inspired video by Doug @TMW (http://blogs.mathworks.com/videos/) this solution is very easy.
enjoy!
25 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
>> j = [1,6,6;1,2,3;6,5,4]
j =
1 6 6
1 2 3
6 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 one occurence, that is; even if one
row contains 1, 2 or 3 sixes it should only return 1. Is this possible? 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 =
ans =
Sorry for the post, it seems to have trouble interpreting special characters such as > and <. I hope it is readable
anyway.
What I am asking is whether there is any way to count each row in a matrix once if it contains at least one six for
example.
Hi Matt,
Multiple ways to do this, but i think the most elegant way is to use the ‘find’ command. I switched your ‘j’ variable to
‘a’ because ‘j’ is a MATLAB variable for the imaginary number 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
hi
i want to add one column of zero whenever column of zero is exist
101101
202202
303303
505505
to
10011001
20022002
30033003
26 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
50055005
any body know how to do that ??
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?
%index contains indices of elements within myVector which are greater than 5
index = find(myVector > 5);
myVector(index) = []
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? Let’s say I have 3 of these
matrices in A(:,:,1), A(:,:,2) and A(:,:,3) and I want to get rid of the zeros from all of these matrices.
Emmi,
the most recent posts in the comment area show this pretty well. Try those out and get back with any other questions.
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!
Hi,
27 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
I have a question on removing multiple rows from a matrix. I knew we can remove rows that we want but specifying
all rows index. But my matrix is quite big and how I can remove 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 to make a simple loop calling the
indicesToRemove?
hi,
i want to remove multiple lines (7*i) index ,But my array is big (4461X1)
Thanks .
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?
@sonny/fado,
I would do it like this:
A=eye(100);
for i=2:2:100
A(i,:)=[];
end
matrix=magic(5);
vector=matrix(:);
You want to use logical indexing. Instead of using the ‘for’ loop, as bertus did, you simply index into the matrix all at
once.
Instead of
A=eye(100);
for i=2:2:100
A(i,:)=[];
end
A = eye(100);
A(2:2:end,:) = [];
28 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Keep in mind that my solution works for any size matrix to get rid of the even rows. For odd rows you would write
A(1:2:end,:). For columns you call them A(:,2:2:end). It keeps going for higher dimensions.
Im not entirely sure about fado’s 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,:) = [];
Greetings:
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 =
1
1.1
1.2
1.3
1.8
1.9
2
2.1
2.2
2.3
2.8
2.9
3
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
What I need however is to include the first element in the subsection that is not included with the find. So for example:
C =
1
1.1
1.2
1.3
1.3 % <-- first element in the array associated with the index that is not included u
1.8
1.9
2
2.1
2.2
2.3
2.4 % <--- first element in the array associated with the index that is not included
2.8
2.9
29 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
3
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
I’m having some trouble explaining this so I hope it make sense. Please email if you can help. And great site by the
way!!!
yup, lost. dunno what your ‘group’ and ’subsection’s are referring to.
idx = find(A>=B);
idy = find(A<B);
returns the indicies from A where A is greater than B. Let’s say for example the above code returns the indicies:
idx =
1
2
3
4
10
11
12
16
17
18
idy =
5
6
7
8
9
14
15
What I need is to return the first index in each excluded group - the first element in each group where A < B. Call this
situation X. So for example:
idx =
1
2
3
4
5 <--- This is X
10
11
12
13 <--- This is X
30 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
16
17
18
So index number 5 and index number 13 appear where A < B but they are included in the vector. 5 and 13 are
elements in idy.
Ahh I think I see that. I’m guessing there are multiple ways to do it but I’ll try to explain the ‘how’ of what I’d do.
Key: Keep everything in terms of indices until the very end since that’s what you are comparing. And then the very
last step should be to pull in the value of the specific indices to your solution vector.
My method involves checking to find increments of 1 in your indices and returns the index + 1 which are your desired
additions (5 and 13 in your example). The final line adds 5 and 13 into your idx index array. One note: the final line
only allows for 2 new indices to be added. If you know how many, you should be able to follow the pattern. If it is
variable, I would recommend using another for loop (1:number of new indices) and adding each new one to idx every
iteration.
vi=[];
for n=2:length(idx)
if idx(n) > idx(n-1)+1
vi=[vi n-1];
else
end
end
m=idx(vi)+1;
Hopefully that’s a start and you can expand from their to your desired end goal. There might be a more elegant way of
doing it, but it should do the trick.
Best,
Zane
Jason,
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(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
Wow, thanks guys. Both solutions work but like SPQR says, the second one is a bit more robust for my purposes. I’m
still working to implement the code so I’m not sure if it will work as I hope (albeit the code provided here does
EXACTLY what I asked for).
31 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Thanks.
My question
For example:
A =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
... (hundreds of numbers)
Andre,
This can be done quite easily, you want to sample your column vector every n index values. It looks like your solution
starts at 1 and then gets every ‘4′ after that, not 5. That’s an easy fix regardless.
A=1:2000; %creates your vector from 1-2000 (as an example), same as 1:1:2000
sol=A(1:4:end); %starts at 1 and captures every 4th value after that until end of vector A
Hi all,
I need the matlab code for duplicate elimination in an array…
kindly 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);
32 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
for i=2:l
for j=1:k
if 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..
anita,
a=[1 1 1 3 6 6 5 7 7 7 7 ];
b = unique(a)
I have a one column vector that contains a series of azimuthal angles between 0 and 180
ex.
Azimuth=
0
10
156
26
120
56
180
etc…
I would like to replace all the values between 90 and 135 with 135.
Here ya go Dingo:
Thanks a lot, i have been slogging over filtering an array and a cellarray of strings. i did something to get the position of
33 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
the elements that will stay ,in an array and had no idea how can i keep only those elements whose position was
recorded and your vector tutorial gave me the idea
thanks a lot
hi,
how can i extract and change data from a multi-dimensional matrix?
i can create a matrix containing all zeros or ones using v=ones(8,8,8); then i need to modify some of the elements using
conditional statement depending upon the position of the elements.
Tapos, you’ll have to be a bit more specific. Do you have data or a more detailed need? If you want to call an entire
row, column, or depth value, the colon ‘:’ mean ‘all’.
etc…
Just keep playing around with those and look at other comments/questions to hopefully find your answer.
good luck.
How do you remove all even integers out of a vector. Lets Say
vec=[45 8 2 6 98 55 45 -48 75]
John. S.,
Best,
Zane
34 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
how is it that as we run our for loop, the values of an array are appended? lets say we have a nested function, how do
we create arrays as the for loop proceeds?
If you want to start from a blank array, then fill it with a new value every iteration try this:
xx=[] %preallocate your answer vector 'xx'
good luck,
Zane
Greetings all:
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;
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 each other (e.g. TS0 < TS1 == True).
I’m using the minutes and seconds only for sake of brevety (e.g. 59 is 59 seconds after the hour/minute; 1727 is 17
minutes 27 seconds after the hour, etc)
35 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
The return value must be a third matrix, nm, where the first timestamp column will survive and the remaining columns
will be joined in one of two ways:
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 not
y21, x12, x13, x14, x15, y22, y23, y24; % element in y changes; x does not
y41, x12, x13, x14, x15, y42, y43, y44; % element in y changes; x does not
x21, x22, x23, x24, x25, y42, y43, y44; % element in y does not change; x does
x41, x42, x43, x44, x45, y42, y43, y44; % element in y does not change; x does
]
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, y14
x21, x22, x23, x24, x25, y42, y43, y44
]
I’ve made far too many attempts to post code here, but essentially I’ve been able to return a 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”.
Jason, i’m trying to go through the logic here but it’s a bit confusing not knowing the problem or the same level of
detail you do. I’m not sure how you’re comparing your matrices or how you get the desired matrix value you want
(one confusion is your ys matrix is 5×4, but your variable ys matrix is 5×5 while all other inputs are 5×5 too; are you
missing a column in ys?). It looks like you need some help creating the logicals of the comparator and then apply that
to concatenating 2 matrices in different ways depending on the logical value.
Summary. I’m not sure how your values are ‘changed’ in the following sentences:
36 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
“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.”
ttys.
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 goal of 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 cannot compare them arbitrarily by lining
them in up in series. So the two methods I outlined are ways to join the series in ways suitable for regression analysis.
For sake of argument, use the first two columns of the matricies - column 1 representing a 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 price 21.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 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 at least the next x change at
time 1840
159 from y
1227 from y
1340 from y
1452 from y
1840 from x
2252 from x
2445 from x
2945 from y
The only time we record a price is when one or the other change from the previous value.
I’ll check that out and edit this post if/when i get a chance, but in the meantime, you can post on the MATLAB Central
(http://www.mathworks.com/matlabcentral/newsreader/) forums where magnitudes more people will view your
37 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
question
Ha ha I did and got no response… I’ve come to a workable solution altering the program elsewhere, thanks for the
response…
Then, I used this for loop below, but it does obtain a row from the matrix, but it is same row every tim
for i = 1:size(d)
indxr = d(indx(i),:);
end
Do you have any solution or idea on how I can get it to work. The basic idea is to randomly select
hi..
This is SHAPS..
I hav a array of strings stored in a variable ‘A’ within a if loop.. this variable is getting overwritten everytime.. I want it
to be stored seperately.. I mean first time when it enters the if loop it shd be stored in A0 next tym when it enters it shd
be in A1 and so on.. how can i do it?
can you pls help..
hi.. this is shaps.. i hav a array of strings stored in a variable ‘A’ within a if loop.. this variable is getting overwritten
evytime.. i want the value of ‘A’ to be stored during each iteration of if loop in the form of A0,A1,A2.. i mean A0
should hav first set of data then A2 next set of data.. how can i do it.. can you pls help..
hi..
This is shaps..
I hav a array of strings stored in a variable ‘A’ within a if loop.. this variable is getting overwritten everytime.. I want it
to be stored seperately.. Like during the first iteration the values shd be stored in A0 and next iteration it should be in
A1 and so on.. How can i do it? can you pls help me..
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
38 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
[1 3 5 7; 2 6 10 14; 3 9 15 21]
Jacob,
Your matrix is composed of multiples of first row (or column). Suppose that the first column is c (given), then your
matrix is:
A=c*(1:numel(c));
A=(1:numel(r)).'*r;
how to display the maximum and minimum group elements in a duplicate vector using matlab code
if the element present in the max group display corresponding max group elements
and
if the element present in the min group display corresponding min group elements
how?Help me
Hello All,
I have an algorithm that finds out the best features in a matrix and gives out the output as the indices of the input data
that were selected as best features.
I want to compare the indices obtained as the output to the input indices and if they match I want to copy the rest of
the row in a separate matrix.
For example:
Input
Col 1 2 3 4…………………
A=[ 100 20 3 4 5 6 7 8---->row1
9 10 11 12 13 14 15 16----->row 2
24 67 21 38 42 53 54 90]—> row 3
39 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Kajal
Kajal,
I’m going to assume you only need help making matrix C and you know how to build your input/output matrices?
In the code, I have some A (input) and B (output) matrices already populated which hopefully you can do already. B
contains the arbitrary number of indices in ‘A’ you want to 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];
for n=1:length(B)
C=[C; A(:,B(n))']; %Add a new row to C using the column of A defined by index B
end
C %prints out C
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
Hi all, I do have a vector with real data that was colected every 5 minutes. i want to have a matrix with data every one
minute. initial matrix A=[1 2 3]; what I want is B=[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3].
Any advice on how to do it?
thanks!
Hi
I have a vector with both +ve and -ve values and the index vector. Both are taken from a large vector. While sorting
the vector again according to -ve to positive I would like to keep track of the index also and arrange it
accordingly..Please let me know
thanks
Hi…
I 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
x3 = [0.6154 0.4057 0.7919 0.9355 0.9218 0.9169 0.7382 0.4103 0.1763 0.8936]? I don’t want to use ‘for’…
Thanks.
hi,
40 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
QQ=cat(3,w,q)
QQ(:,:,1) =
12
34
QQ(:,:,2) =
56
78
now 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 for 3D arrays?
QUESTION
How can I remove rows in a matrix that have third element in the column 0
Input-
123
230
023
020
ANSWER
c =
1 2 3
2 3 0
0 2 3
0 2 0
>> z=find(c(:,3)==0)
z =
2
4
>> c(z',:)=[]
c =
1 2 3
0 2 3
Answered this question by reading this page alone.. Thanks for the post..
New Comer to this page.
41 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
c=
123
230
023
020
>> z=find(c(:,3)==0)
z=
2
4
>> c(z’,:)=[]
c=
123
023
hi,
I am trying to implement linear convolution by my own, as a part of class project.
my query is
let
a=[1 2 3; 4 5 6;7 8 9]
hi,
I am trying to implement linear convolution by my own, as a part of class project.
my query is
let
a=[1 2 3; 4 5 6;7 8 9]
a(6), displays the element of 6, but I would like to know , how to make it display the entire row/column .I wish to
display only the second row. i.e 4 5 6.
I’ve 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
42 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
example:
i have data on the 2500000 and below…after the 2500000th column - i only have zeros.
question:
how do i get rid of all the columns from 2500000 - 4000000 without getting the “out of memory” error.
tried:
matirx(:,2500001:end)=[];
newmatrix=matrix(:,1:2500000);
i can’t even copy the old matrix with a new one without getting the “out of memory error”. Please help, thanks!
hi,
i need a matlab code which can store pixel values in a register or a 1D array.
and
i need code for converting m*m matrix to m*1 row matrix
Hi All,
i have a matrix
A=[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, 81
81, 81, 81, 81, 81]
i have to find the frequency of repeated elements as well as distinct element list like
b = 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81 // array of distinct elements
freq = 1, 1, 1, 2, 1, 4, 3, 3, 4, 7, 4, 7 // frequency of each distinct element
Dear all,
do you have any idea how could I accumulate every three member of an array and save it t o another array:
Lets assume:
A=[1;2;3;4;5;6;7;8;9]
B=[1+2+3;4+5+6;7+8+9]
Thanks
The answer:
B= kron(speye(3),ones(1,3))*A;
43 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Hi there!
Is there a function that would enable me to find the proportion of values in an array that are above or below a certain
value?
Thanks, Liz
Hi all,
How do I repack the one column vector(with 200 elements) into 50 column vectors with 4 elements each? Also, I need
to have those new vectors as a part of one matrix.
Thank you.
dear all,
i want to make a 6×6 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.
Leave a Reply
<pre lang="MATLAB">
</pre>
Name
Mail (hidden)
Website
44 of 45 3/25/2011 1:43 AM
Matlab - Array Manipulation and Tricks | blinkdagger http://blinkdagger.com/matlab/matlab-tips-and-tricks-on-manipulating-1-d...
Links
The MathWorks Blog Ring
Undocumented MATLAB Tips
Data Mining in MATLAB
I want an iPad
Search for:
Recent Posts
The End of Blinkdagger? . . . . Possibly
MATLAB - Global Variables
MMM #34 Winner, Sander Land!!!
Monday Math Madness #34: Crossing a Bridge
MATLAB GUI Tutorial - UITABLE Part 2, How To Access Table Data
45 of 45 3/25/2011 1:43 AM