Segmenting a Vector into Rows of 8 Characters in MATLAB?

In summary, the user is struggling with segmenting a vector's characters into a new vector with 8 columns and as many rows as needed. They have figured out how to make the input vector a multiple of 8 characters in length, but are struggling with the segmentation process. They have tried using a for loop and the reshape function, but have encountered issues with the length of the original vector and the direction of the reshape. They are looking for help on how to properly segment the vector into a 2x8 matrix.
  • #1
GreenPrint
1,196
0
Given an arbitrary one dimensional vector v imputed by the user
for example v='Helllllloooooo'
or
v='Helo'
I was able to append empty characters onto anyone dimensional vector that the user can enter so that way the total length of the vector is a multiple of 8 characters of long or not add any empty characters onto the vector if it's length is already an exact multiple of 8

I'm struggling with segmenting this vector's characters to a vector that has as many rows as needed that each have exactly 8 column's

for example
function('Hello World!')
and the function returns
g=Hello Wo
rld!
were it adds blank characters so that there is actually blank spaces after the exclamation mark to make the second row 8 characters in length.

I'm all set on making the input to the function be a multiple of 8 characters in length regardless of what string the user inputs. I'm struggling with segmenting this new message like in the example above because the message can be any multiple of 8... I tried using a four loop and just taking off multiples of 8 and setting them to a new row of another vector but am struggling because of the fact that the message can be any length

basically how do I turn any string who's length is a multiple of 8 (i figured out how to append blank spaces so that it would always be a multiple of 8) and segment it into something like this
function('Hello World!')
and the function returns
g=Hello Wo
rld!

I hope that makes sense
thanks for any help
if it's not clear what I'm trying to do please let me know.

I tried taking any string such as
V='helloooooooooo '
and using
reshape(V,2,8)
but it returns something like this

hlooooo
elooo

which is not what i wanted...
 
Physics news on Phys.org
  • #2
So you want g to be a 2x8 matrix in the case you listed?
The reshape you used there won't work as reshape goes down columns, not across rows. To fix this though, use reshape(V,8,2)' which gives you the transpose of the reshape vector.
Don't forget to pad out the vector so it's the right size beforehand though!
 

Related to Segmenting a Vector into Rows of 8 Characters in MATLAB?

1. What is MATLAB?

MATLAB is a high-level programming language and interactive environment used for scientific computing, data analysis, and visualization. It allows users to manipulate matrices and arrays, implement algorithms, and create user interfaces.

2. How is MATLAB used in computer science?

MATLAB is commonly used in computer science for tasks such as data analysis, image processing, machine learning, and numerical computing. It is also used in research and development for building and testing algorithms and models.

3. Can MATLAB be used for developing software?

While MATLAB is primarily used for scientific and numerical computing, it can also be used for developing software. Its built-in functions and libraries make it an efficient tool for prototyping and testing algorithms, and it also has capabilities for creating GUIs and web applications.

4. Is MATLAB difficult to learn?

Learning any programming language takes time and practice, but MATLAB is generally considered to be relatively easy to learn. Its intuitive syntax and extensive documentation make it accessible to beginners, and there are also many online resources and tutorials available.

5. Can MATLAB be used for data analysis and visualization?

Yes, MATLAB is widely used for data analysis and visualization due to its powerful built-in functions and plotting capabilities. It allows users to import, manipulate, and analyze data from various sources and create visual representations such as graphs, charts, and maps.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top