Matlab Cody (1)
7091 단어 matlab
What is Cody? see here
Problem 1. Times 2 - START HERE
function y = times2(x) y = bitshift(x,1); % size=13 end function y = times2(x) y = 2*x; % size=12 (same as y=x+x) end function ans = times2(x) 2*x; % size=10 (best) end Problem 2. Make the vector [1 2 3 4 5 6 7 8 9 10]
function x = oneToTen x = [1 2 3 4 5 6 7 8 9 10]; % size=20 end function x = oneToTen x = 1:10; % size=11 end function ans = oneToTen 1:10; % size=9 (best) end function ans = oneToTen colon(1,10); % size=10 end function ans = oneToTen linspace(1,10,10); % size=11 end function x = oneToTen x = cumsum(ones(1,10)); % size=14 end Problem 3. Find the sum of all the numbers of the input vector
function ans = vecsum(x) sum(x); % size=10 (best) end function y = vecsum(x) y = sum(x(:)); % size=14 (useful for matrix) end function y = vecsum(x) n = length(x); y = 0; for i = 1:n y = y + x(i); % size=30 end end Problem 4. Make a checkerboard matrix
function a = checkerboard(n) a(1:2:n,1:2:n) = 1; a(2:2:n,2:2:n) = 1; % size=36 end function ans = checkerboard(n) invhilb(n)>0; % size=12 (best) end function a = checkerboard(n) a1 = repmat([1,0;0,1],n,n); a = a1(1:n,1:n); % size=31 end function a = checkerboard(n) for i=1:n for j=1:n a(i,j)=mod(i+j+1,2); % size=32 end; end; end Problem 5. Triangle Numbers
function ans = triangle(n) sum(1:n); % size=12 (best) end function ans = triangle(n) n*(n+1)/2; % size=15 end Problem 6. Select every other element of a vector
function ans = everyOther(x) x(1:2:end); % size=15 (best) end function ans = everyOther(x) x(1:2:length(x)); % size=16 (same as x(1:2:numel(x))) end function x = everyOther(x) x(2:2:end) = []; % size=18 end ps: m-by-n의 매트릭스 x에 대해numel(x)=m*n,length(x)=.
Problem 7. Column Removal
function B = column_removal(A,n) A(:,n) = []; B = A; % size=19 end function A = column_removal(A,n) A(:,n) = []; % size=15 end function A = column_removal(A,n) A(:,n) = ''; % size=14 (best) end function ans = column_removal(A,n) A(:,[1:n-1,n+1:end]); % size=24 end function ans = column_removal(A,n) A(:,1:end ~= n); % size=17 end Problem 8. Add two numbers
function ans = add_two_numbers(a,b) a+b; % size=11 end Problem 9. Who Has the Most Change?
function a = most_change(a) [~,a]=max(a*[.25;.05;.1;.01]); % size=24 end function b = most_change(a) [~,b]=max(sum(bsxfun(@times,a,str2num('[0.25,0.05,0.10,0.01]')),2)); % size=24 end function b = most_change(a) total = a * [25 5 10 1]'; b = find(total==max(total)); % size=28 end function b = most_change(a) [~,b] = max(a * str2num('[0.25; 0.05; 0.1; 0.01]')); % size=18 (best) end Problem 10. Determine whether a vector is monotonically increasing
function ans = mono_increase(x) isequal(unique(x), x); % size=13 (best) end function ans = mono_increase(x) mean(diff(x)>0)==1 | isempty(diff(x))==1; % size=24 end function ans = mono_increase(x) all(diff(x)>0); % size=14 end Problem 26. Determine if input is odd
function ans = is_it_odd(n) bitand(n,1); % size=11 (best) end function ans = is_it_odd(n) mod(n,2); % size=11 (best) end function ans = is_it_odd(n) rem(n,2); % size=11 (best) end function ans = is_it_odd(n) 1-(round(n/2)==n/2); % size=19 end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【MATLAB】변수를 스크립트에 저장MATLAB에서 계산 결과를 저장할 때 MAT 파일, Excel 등의 파일에 저장하는 것이 좋을 것이라고 생각하지만 변수의 유형에 따라 스크립트에 내보낼 수 있습니다. MATLAB을 사용해 10년 정도가 됩니다만, ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.