Borrar filtros
Borrar filtros

How do I make this a function?

3 visualizaciones (últimos 30 días)
hbcukid
hbcukid el 6 de Jun. de 2021
Editada: per isakson el 7 de Jun. de 2021
I am fairly new to Matlab and am new to making functions, I have code for a noncircular shift and am trying to make it a generalized function.
A = rand(100,100);
Fill_Value = 0;
for Target_Group = 1: +5: length(A)
for Relative_Row = 0: 4
Row = Target_Group+Relative_Row;
Shift_Value = Relative_Row* 10 ;
A(Row,:) = [Fill_Value*ones(1,Shift_Value) A(Row,1:end-Shift_Value)];
end
end
I am trying to make this a function where A(a multidimensional array), fill_value(int), shift_value(int), and 10(int) can be represented as parameters. How do I do it?
inputs - A:a multidimensional array, fill_value: an int, shift_value: an int, and 10: an int; output would be A with the aforementioned code performing a non circular shift

Respuesta aceptada

per isakson
per isakson el 6 de Jun. de 2021
Editada: per isakson el 7 de Jun. de 2021
Something like this. With Matlab it's common practise to use double for all numerical variables. (Matlab has "integer in floating-point format" called flint, which isn't affected by round-off errors. See Floating Points in Matlab.)
A = rand(100,100);
Fill_Value = int16( 0 );
ten = int16( 10 );
Shift_value = int16( ?? );
A = noncircular_shift( A, Fill_value, Shift_value, ten )
where
function A = noncircular_shift( A, Fill_value, Shift_value, ten )
for Target_Group = 1: +5: length(A)
for Relative_Row = 0: 4
Row = Target_Group+Relative_Row;
Shift_Value = Relative_Row* ten ; % notice ten
A(Row,:) = [Fill_Value*ones(1,Shift_Value), A(Row,1:end-Shift_Value)];
end
end
end
Notice that the loop-counters will be doubles (i.e flints).

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by