I wrote a code but when i am trying to change the variable schedule(2,:) it gives error. Here's the code:
clc;clear;
a = [1 2 3 4];
N = 3;
c=[1:12];
schedule(1,:) = kron(a,ones(1,N));% repeat 4 days
schedule(2,:) = repmat([1 2 3],1,((numel(c)/length(a)))+1); % repeat time slots in each day %nums col rep
schedule(3,:) = randperm(c(1,end)); % randomize 12 courses
schedule
I need a way of matching lengths of schedule(2,:) with other rows. When length of other rows is 20, schedule(2,:) does not build more than 20.
clc;clear;
day = [1 2 3 4];
n=length(day);
time=[1 2 3];
a=length(time);
schedule(1,:) = kron(day(1):n,ones(1,a));
schedule(2,:) = repmat(time,1,n);
schedule(3,:) = randperm(120,length(schedule(2,:)));
schedule
i have completed my code myself haha :) it is aflexible matrix and can accept any changes awithout errors
Related
I would like to generate a continuous number with repetitive increment, for example,from 1 to 3. with a repetitive increment of 1 (x 5). so the output will be.
output =
[1
1
1
1
2
2
2
2
2
3
3
3
3
3]
five repetition of 1, then five repetition of 2 and so on.
I tried this code:
a = [1:1:3]
for i = a(:,1:end)
disp(i+zeros(5,1))
end
I got the same result, however, I can't put the output in one column. Thanks for the help.
Let
n = 3; % number of distinct numbers
s = 2; % starting number
m = 5; % number of repetitions of each number
A couple of options are
output = repelem(s:s+n-1,m);
or
output = ceil(s-1+1/m:1/m:s+n-1);
I have matrices A=2x2 B=2x4 C=2x2 I want result in a matrix D=2x16 for example
A=[1 3;
2 4]
B=[3 2 4 2;
4 3 6 3]
C=[4 5;
7 5]
D=[(1+3+4) (1+3+5) (1+2+4) (1+2+5) (1+4+4) (1+4+5) (1+2+4) (1+2+5) (3+3+4) (3+3+5) (3+2+4) (3+2+5) (3+4+4) (3+4+5) (3+2+4) (3+2+5);
(2+4+7) (2+4+5) (2+3+7) (2+3+5) (2+6+7) (2+6+5) (2+3+7) (2+3+5) (4+4+7) (4+4+5) (4+3+7) (4+3+5) (4+6+7) (4+6+5) (4+3+7) (4+3+5)]
means that row wise elements are added in resultant. I have a code in which A=2x1 but I can understand how to mold it according to A=2x2 the code is given below
[rows,col_B]=size(B);
[~,col_C]=size(C);
result=zeros(rows,col_B*col_C);
for i=1:col_B
for j=1:col_C
result(:,(i-1)*col_C+j)=A+B(:,i)+C(:,j);
end
end
can anybody tell me the syntax for the output RESULT in this code?
Try
D = kron(A,ones(1,size(B,2)*size(C,2)))+repmat(kron(B,ones(1,size(C,2))),1,size(A,2))+repmat(C,1,size(B,2)*size(A,2))
Explained: kron(Mat, ones(1,num2RepeatColumns)), repeats each column of the matrix, Mat. So "repeating" twice makes A = [1 2; 3 4] becomes A = [1 1 2 2; 3 3 4 4], see Matlab: repeat every column sequentially n times
repmat(Mat, 1, num2RepeatMatrix) copies the whole matrix, Mat and "pastes" it right next to the original, so A=[1 2; 3 4] becomes A = [1 2 1 2; 3 4 3 4]. See "Horizontal Stack" example of repmat: https://www.mathworks.com/help/matlab/ref/repmat.html
OLD
D=[];
for ii=1:size(A,2)
D=[D,A(:,ii)+kron(B,ones(1,size(C,2)))+repmat(C,1,size(B,2))];
end
I have written an algorithm that finds the local maxima and minima in a signal.
[id_max, id_min] = find_max_min(signal);
I would like now to check:
if the alterantion of maxima and minima is respected
i.e. id_max(1)<id_min(1)<id_max(2)<id_min(2)<...
we could start with a minimum..this is not known
Suppose that:
id_max = [1 3 5 7 10 14 20];
id_min = [2 4 6 8 16 19];
I would like to have 2 vectors missing_max missing_min indicating the location of the missing maxima and minima.
A missing maximum (minimum) occours when between two consecutive minima (maxima) in id_min (id_max) there is not a maximum (minimum).
In this example a maximum is missing in the 7th position of id_max because in id_min there are two consecutive values (16 19) without a maximum between.
Then we have
missing_max = [7]
missing_min = [5]
since
id_max = [1 3 5 7 10 14 X 20];
id_min = [2 4 6 8 X 16 19]; (with X I marked the missing values)
If the alternation is correct the vectors should be empty. Can you suggest an efficient way to do that without for loops?
Thanks in advance
Here's a script that you can adapt to a function if you want:
id_max = [1 3 5 7 10 14 20];
id_min = [2 4 6 8 16 19];
% Group all values, codify extremity (1-max, 0-min), and position
id_all = [ id_max, id_min ];
code_all = [ones(size(id_max)), zeros(size(id_min))];
posn_all = [ 1:numel(id_max), 1:numel(id_min) ];
% Reshuffle the codes and positions according to sorted IDs of min/max
[~, ix] = sort(id_all);
code_all = code_all(ix);
posn_all = posn_all(ix);
% Find adjacent IDs that have the same code, i.e. code diff = 0
code_diff = (diff(code_all)==0);
% Get the indices of same-code neighbors, and their original positions
ix_missing_min = find([code_diff,false] & (code_all==1));
ix_missing_max = find([code_diff,false] & (code_all==0));
missing_min = posn_all(ix_missing_min+1);
missing_max = posn_all(ix_missing_max+1);
Caveats on IDs:
Make sure your id_min and id_max are rows (even if empty);
Make sure that at least one of them is not empty;
While they need not to be sorted, their values must be unique (within the IDs and across).
Later edit:
New version of the code, based on new explanations about the definition:
id_max = [1 3 5 7 10 14 20];
id_min = [2 4 6 8 16 19];
%id_max = [12 14]
%id_min = [2 4 6 8 10];
id_min_ext = [-Inf, id_min];
id_max_ext = [-Inf, id_max];
% Group all values, and codify their extremity (1-max, 0-min), and position
id_all = [ id_max_ext, id_min_ext ];
code_all = [ones(size(id_max_ext)), zeros(size(id_min_ext))];
posn_all = [ 0:numel(id_max), 0:numel(id_min) ];
% Reshuffle the codes and position according to sorted positions of min/max
[~, ix] = sort(id_all);
code_all = code_all(ix);
posn_all = posn_all(ix);
% Find adjacent IDs that have the same code, i.e. code diff = 0
code_diff = (diff(code_all)==0);
% Get the indices of same-code neighbours, and their original positions
ix_missing_min = find([code_diff,false] & (code_all==1));
ix_missing_max = find([code_diff,false] & (code_all==0));
missing_min = unique(posn_all(ix_missing_min-1))+1;
missing_max = unique(posn_all(ix_missing_max-1))+1;
However, the code contains a subtle bug. The bug will be removed by either the person that asked the question, or by me after he/she improves the question in such a way that is really clear what's asked for. :-) Due the fact that we have 2 virtual extremums (one max and one min, at ID = −∞) is possible that the first missing extremum will be marked twice: once at −∞ and once at the first element of the ID list. unique() will take care of that (though is too much of a function call to check if the first 2 elements of an array have the same value)
I want to raise a matrix to a next matrix and subtrat one before taking the product.
e.g.
A = [2 3 5
2 3 0]
B = [2 2 1
1 2 0]
so prod(A.^B-1) would be:
first row (2^2-1)*(3^2-1)*(5^1-1)=96
second row (2^1-1)*(3^2-1)=8
and we would have prod(A.^B-1) = 96, 81. the trick also to skip past the zero, i keep getting zero or NaN, i think the zero is being calculated as well.
Is there a way to code this,
this is the code I have in mind
if A~=0 && B~=0
prod(A.^B-1)
end
You could do it like this using logical indexing to replace instances where A.^B-1 is 0:
A = [2 3 5;2 3 0];
B = [2 2 1;1 2 0];
C = A.^B-1;
C(C==0) = 1; % Replace zeros with ones
D = prod(C,2) % Product across the columns
which returns
D =
96
8
provided that you remove the zeros, I don't think that you should get NaN unless your original matrices contain it. However, You can replace it in the same manner as well (C(isnan(C)) = 1;).
I am trying to solve a MATLAB problem to generate a vector like 1,2,2,3,3,3,4,4,4,4...
So if n = 3, then return
[1 2 2 3 3 3]
And if n = 5, then return
[1 2 2 3 3 3 4 4 4 4 5 5 5 5 5]
This is what I came up with:
ans=1
for n=2:n
ans=[ans n*ones(1,n)]
end
But I'm trying to minimize the code length. Anyone have any ideas?
still a few lines:
n = 5; %number of elements
A(cumsum(0:n)+1) = 1;
B = cumsum(A(1:end-1))
returns
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
In the same spirit, here's my one liner:
nonzeros(triu(meshgrid(1:n)))'
n = 5;
A = triu(ones(n,1)*(1:n));
A(A==0) = [];
This is similar to jkshah's answer, but I would approach it slightly differently,
n=5;
M = ones(n,1)*(1:n)
B = M(triu(ones(n))>0)';
Here's another one-liner. Unlike solutions based on triu, this one doesn't generate extra elements as intermediate results (that doesn't mean it's faster, though):
fliplr(cumsum([n full(sparse(ones(1,n-1),cumsum(n:-1:2),-1))]))
A little 'magic' solution:
ceil(sqrt(2*(1:(n^2+n)/2))-0.5)
See visualisation:
This is the plot of function sqrt(2*(1:(n^2+n)/2))-0.5:
plot(1:(n^2+n)/2,sqrt(2*(1:(n^2+n)/2))-0.5,'.')
where xticklabels were changed according the following code:
set(gca,'xtick',cumsum(0:n),'xticklabel',0:n)
it is a litle bit longer
function y = your_fcn_name(n)
N = sum(1:n);w = [] ;
for i=1:n
q(1:i) = i;
w = [w q(1:i)];
end
y = w;
end