How to vectorize a loop in matlab [duplicate] - matlab

This question already has answers here:
Structure initialization with repmat
(2 answers)
Closed 9 years ago.
I want to use a function repmat to write this code:
for j=1:30
for i=1:10
myObject{i,j}.s = zeros(6,1);
end
end
I cannot understand how to do that for cells. Can anyone help me please?

You can use deal:
[myObject{1:10,1:30}] = deal( struct('s',zeros(6,1) );
PS: It is best not to use i and j as variables in Matlab.

Related

How to make MATLAB start the xtick from the very beginning? [duplicate]

This question already has answers here:
limit the axes for plotting in Matlab [duplicate]
(2 answers)
Closed 3 years ago.
I am plotting with MATLAB and make MATLAB to put 15 xticks on the x-label. The first xtick doesn't start from the very beginning, though.
xticks([1:15]);
xticklabels({'(1, 2)','(1, 3)','(2, 3)','(1, 4)','(2, 4)','(3, 4)','(1, 5)','(2, 5)','(3, 5)','(4, 5)',...
'(1, 6)', '(2, 6)', '(3, 6)', '(4, 6)', '(5, 6)'});
All you need is: xlim([1:15]);

How to convert E numbers in MatLab [duplicate]

This question already has answers here:
Avoiding scientific notation with Matlab
(2 answers)
Closed 6 years ago.
I created a loop using tic toc in MatLab with the below code
tic
for i = val52
val50(i)=i;
end
toc
i then divide the result by 128 (samply frequency) however i get a number which says 2.0516e-05how can I convert this number into a normal number?
If you do not want the e symbol then you can use fprintf instead of disp:
a= 1/12345678;
formatSpec = 'a is %8.13f \n';
fprintf(formatSpec,a)
>> a is 0.0000000810000

MATLAB: how to reuse plot options? [duplicate]

This question already has answers here:
Is that possible to assemble several options and pass to the plot function in matlab
(4 answers)
Closed 7 years ago.
I have the following plot:
patch('Vertices',rocket_point_cloud,'Faces',rocket_faces,...
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1)
I would like to reuse the plot options, i.e. reuse:
'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1
Is it somehow possible to store the above in a variable, e.g. my_options and later on do:
patch('Vertices',other_cloud,'Faces',other_faces,my_options)
Thanks for your help!
Sure. Just define your options in a cell array,
my_options = {'FaceColor','red','EdgeColor','none',...
'BackFaceLighting','reverselit',...
'SpecularStrength',1,'DiffuseStrength',1};
and then expand that cell array into a comma-separated list via curly-brace indexing:
patch('Vertices', rocket_point_cloud, 'Faces', rocket_faces, my_options{:})

how to crop image in matlab using only for loop? [duplicate]

This question already has an answer here:
Zoom and Crop Image (Matrix)
(1 answer)
Closed 7 years ago.
i made it with Matrix.how can i make it with for loop
crop.m
function s=crop(f,m,n,x,y)
s=f(x:x+m,y:y+n);
this is code
crop1.m
function s=crop1(f,m,n,x,y)
for i=x:1:x+m
for j=y:1:y+n
s(i-x+1,j-y+1,:)=f(i,j,:);
end
end

Access second output of Matlab procedure directly [duplicate]

This question already has answers here:
How to elegantly ignore some return values of a MATLAB function
(8 answers)
How do I get the second return value from a function without using temporary variables?
(2 answers)
Closed 9 years ago.
I'm not very good at programming and new to matlab, so sorry if I'm not using the right terminology.
If I use the e.g. fminbnd procedure in Matlab first i get the x-value which minimises and then i get the function value. Is there a neat way for me to get just the minimum function value.
To make it clear, for me it seems I have to do:
[x,y] = fminbnd(h,-10,10)
when I only need y. Is there any way for me to not get x?
Use ~ to suppress x output. Only available in later versions of matlab (=> r2009b).
[~, y] = fminbnd(h, -10, 10);