Stuart’s MATLAB Videos

Watch and Learn

Making a matrix in a loop in MATLAB 39

Posted by Doug Hull,

A for loop is used to construct a simple matrix with an underlying pattern. Pre-allocation is addressed in the second half of the video.

39 CommentsOldest to Newest

dhull replied on : 2 of 39

@Oleg,

Thank you, you are right and I can see the advantage to knowing that technique.

I, however, have a hard time internalizing and visualizing that kind of construction. Mentally, it takes me a little longer to figure out what bsxfun does. For small matrices like this, I prefer ease of understanding to terseness of code.

For this case, there is no appreciable performance difference, so it is a matter of taste.

FWIW, I do not make up these cases, virtually everything on this blog comes from user questions. If someone is asking me how to make that matrix, then that tells me they are not ready for bsxfun! I only bring out the more powerful (but harder to understand) techniques for the *advanced* section of this blog.

Thanks,
Doug

ravi replied on : 3 of 39

Hi Doug, great and helpful site here!

Here is the beginning of my code;

r2=1
r1=10
a=[r2:r1];
for r2 = r2:r1
  G = r2/3;
  sigrad2 = G*a^2;
end

This is just a simplification of a larger code but for the crux of the matter is, the for loop calculates vectors for sigrad2, but I would like to store each of the results in a matrix – I have watched the video where you build a vector with y = i+rand inside the loop but haven’t been able to extend it to my case.

I would then like to plot each of those rows in the resultant matrix against a

Could you please help me in building that matrix? It seems simple but I can’t figure it out

Thanks in advance

ravi replied on : 5 of 39

@dhull

Yeah I mentioned that I saw that video although that only builds a 1×10 vector but I’m not sure how to expand that code to a mxn matrix

Chris replied on : 7 of 39

I have an array
( -0.2429 -0.4420 -0.4997 -0.3964 -0.1646 0.1209 0.3669 0.4935 0.4606 0.2803 0.0114 -0.2601 -0.4483 -0.4933 -0.3796 -0.1425 0.1420 0.3813 0.4970 0.4520 0.2620 -0.0107 -0.2785 -0.4568 -0.4894 -0.3649 -0.1213 0.1642 0.3986 0.5045 0.4467 0.2452 -0.0330 -0.2973 -0.4642 -0.4821 -0.3461 -0.0984 0.1833 0.4087).

I want to call the first 8 elements and find their average, and the next 8 elements and find their average up till the end and in the process form another array with a reduced 5 elements. e.g (a b c d e)

How do i do this in matlab please?

doug replied on : 8 of 39

@Chris,

You would need to pull out the subsets. A for loop would probably work well.

for i:1:8:whatever
do something
end

MEAN is the function to get the mean.

then storing it in a vector:

count = 0

for i = …
count = count+1;
vec(count) = whatever you save
end

-Doug

Yaacov replied on : 9 of 39

You can use the reshape function to make your array a matrix with 8 columns, then use the mean function to calculate the mean of every row. Finally, transpose (‘) to make it a row column:

A=reshape(A,[],8);
mA=mean(A,1);
mA=mA’;

Monica replied on : 10 of 39

How would I create a 3 by 3 matrix where each element is equal to the square of the sum of its row and column numbers?

manju replied on : 12 of 39

how to create multidimensional array in a single loop statement?? or how to declare multiple arrays??

Pedro replied on : 13 of 39

Sir,

you help me a lot. I have a problem and your loop give me a “light on mind” rs

Thanks a lot !

Good work.

AMIT replied on : 14 of 39

i have two excel files and i am trying to call them in matlab,my aim is to check the value in 1st excel file and accordingly edit second
f=xlsread(‘cbtd.xls’);%/ this file has to be edited later
g=xlsread(‘cbam.xls’);
now i have use two nestd for loops and check a condition that
if g(i)(j)<80 then
f(i)(j)=0

and that excel file named f should be edited
thanks in advance DOUG

Ingrid replied on : 15 of 39

@AMIT: Why do you want to use two for loops? Have a look at this example:
A=magic(5);% Or the matrix from your first Excel file
hv=(A<20); %All elements that are smaller than 20
B=magic(5)*20 %Or the matrix from your second excel file
B(hv)=0;%Change the values of your second file to 0 for which the condition of the first file is true
Of course at the end you need to write B back to the Excel file :-).

AMIT replied on : 16 of 39

thanks ingrid it worked, now i have another problem , i’ve generated BW image and it has some spots my aim is to calculate area of each spot(white)..

Ingrid replied on : 17 of 39

@AMIT: Have a look at bwarea. Btw. there is always technical support that you can contact.

varinder replied on : 18 of 39

function P = legendre(x,n)

P(1) = 1;

P(2) = x;

P(3) = (3*(x^2)- 1)/2;

P =((2.*(n+1)).* x .*(P(n)) – n.*(P(n-1)))./(n+1);

end

test script:

clear legendre;
x = -1;
P = zeros(1,5);

for i = 3:5
P = legendre(x,i);
end

error:

Attempted to access P(4); index out of bounds because numel(P)=3.

Error in legendre (line 7)
P =((2.*(n+1)).* x .*(P(n)) – n.*(P(n-1)))./(n+1);

Error in new_tb (line 6)
P = legendre(x,i);

madhavi replied on : 19 of 39

hi i need help in finding the average using for loop…
( -0.2429 -0.4420 -0.4997 -0.3964 -0.1646 0.1209 0.3669 0.4935 0.4606 0.2803 0.0114 -0.2601 -0.4483 -0.4933 -0.3796 -0.1425 0.1420 0.3813 0.4970 0.4520 0.2620 -0.0107 -0.2785 -0.4568 -0.4894 -0.3649 -0.1213 0.1642 0.3986 0.5045 0.4467 0.2452 -0.0330 -0.2973 -0.4642 -0.4821 -0.3461 -0.0984 0.1833 0.4087).,in this array of data in first instance it must find average of fist five elements ..2nd time it must find average of (2+3+4+5+6) elements average….third time( 3+4+5+6+7)…like this it must keep on finding average till the end of the array…

Stuart McGarrity replied on : 20 of 39

Must to use a loop? Have you written loops before? What do you have so far for code?

subhendu roy replied on : 21 of 39

sir i need to find 25*5 column(total of 125) of a matrix. using for loop. everytime 1st column will be incremented and so on..please help me to find out.the above video shows error like” Subscript indices must either be real positive integers or logicals.”

satyavratan replied on : 22 of 39

sir. i am dividing a 210*210 image in to several 3*3 matrices and performing few operations inside each 3*3. i have created a 3*3 matrix and i have put for loops for row and column operations. i need to operate the same kind of operations for next 3*3 and so on. help me.

sasi replied on : 24 of 39

Algorithm 1 Confusion operation Input: Images Output: Confused Image
for each row ’i’ in the matrix ’I’ do Generate random vector KR for each column ’j’ in the matrix ’I’ do rowsum=rowsum + I [i] [j] end for rowmod=rowsum % 2 if rowmod == 0 then for each KR do I=right circular shift(I) end for else for each KR do I=left circular shift(I) end for end if end for for each column ’j’ in the matrix ’I’ do Generate random vector KC for each row ’i’ in the matrix ’I’ do colsum=colsum + I [i] [j] end for colmod=colsum % 2 if colmod == 0 then for each KC do Iscr=up circular shift(I) end for else for each KC do Iscr=down circular shift(I) end for end if end for

sachin replied on : 26 of 39

Hello sir, I have equations of motion and i want to form a matrix using these equations by only putting the elements of corresponding function.
As i am new to matlab please guide me how to proceed.
Thanks

Stuart McGarrity replied on : 29 of 39

Hi Ken, Do you mean, using MATLAB but not using MATLAB random number functions like rand or randn? If so, can I ask why you would want to do that?

sri replied on : 30 of 39

I want to process image matrix in “for loop” in which first loop should consider the matrix 3* 3 , second loop should consider the matrix 2*2, third loop should consider the matrix 2* 2 and again 3*3 and so on…
i.e.first loop-(1:2,1:3)
second loop-(1:2,4:5)
third loop-(1:2,6:8)
fourth loop-(1:2,9:10) and so on…
Can we able write this as coding in Matlab?

Stuart McGarrity replied on : 31 of 39

Hi Sri, Your first loop iteration has 3 columns, the second 2, the third 3 and the forth 2. Is that correct? So the number of columns is 3 for odd iterations and 2 for even? Or did you make a mistake and it should be 2 each time?

mỹ phẩM Thái Lan replied on : 32 of 39

you’re really a good webmaster. The website loading speed is amazing.

It kind of feels that you are doing any unique trick.
Moreover, The contents are masterpiece. you’ve done a magnificent job on this subject!

M Haroon replied on : 33 of 39

I want To add Two matrices in a script file how can i do that. ?? while matrices are been created by using the “for” loop , based on user’s requirement to input matrix of any order.

site replied on : 35 of 39

Very goоd article! Wе аre linking to thіs gгeat
article ߋn oᥙr site. Keep uр tҺe great writing.

Harshini replied on : 36 of 39

can anybody help me out to how to assign array variable to no. of cropped subimages of a image in a for loop?

ezgi replied on : 38 of 39

I would like to create 500×500 matrix like that [m1 m2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0…… ………………………………..]
[ 0 0 m3 m4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0…… ……………………………….]
[ 0 0 0 0 m5 m6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0…… …………………………….]..
.. …
This is 500×500 matrix like that and I need a loop to construct this matrix in MATLAB.

Thank you

Add A Comment

Your email address will not be published. Required fields are marked *

What is 7 + 9 ?

Preview: hide