I need to convert a vector into a matrix using a function, but I cannot use reshape or any build functions.

Write a MATLABFUNCTIONthat takes an input argument, a 12-element vector array and outputs a 3x4 matrix array. Example, given the input vector, vec = [‘a’ ‘b’ ‘c’ ‘d’ ‘e’‘f’ ‘g’ ‘h’ ‘i’ ‘j’ ‘k’ ‘l’]

2 commentaires

This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
function [mat] = VecToMat(a,b,c,d,e,f,g,h,i,j,k,l)
mat = [a,d,g,j;b,e,h,k;c,f,i,l];
end
It works for numbers, but not for letters

Connectez-vous pour commenter.

Réponses (1)

You need to understand that passing in 12 separate arguments to a function is NOT the same thing as passing in a vector of length 12.
Your function should look like...
function mat = vec2mat(vec)
% code to convert the vector of length 12 into a matrix of size 3x4
% Your job...
end
You might call the function like this
vec = [1 2 3 4 5 6 7 8 9 10 11 12];
mat = vec2mat(vec);

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by