Creating a Matrix with Ones at Each End and Zeros in Between in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter glyvin
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Click For Summary
SUMMARY

The discussion focuses on creating a specific matrix structure in MATLAB, where the matrix consists of ones at each end and zeros in between. The user seeks to generate a vector of the form x=(1,0,...,0,1)T for various sizes without manual input. A solution is provided using MATLAB code that initializes a zero vector and sets the first and last elements to one, effectively creating the desired matrix structure.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with matrix operations in MATLAB
  • Knowledge of control structures such as loops and conditionals in MATLAB
  • Understanding of vector indexing in MATLAB
NEXT STEPS
  • Explore MATLAB's matrix manipulation functions
  • Learn about MATLAB's logical indexing techniques
  • Investigate the use of the 'mod' function in MATLAB for conditional operations
  • Study advanced MATLAB programming concepts such as vectorization for performance optimization
USEFUL FOR

This discussion is beneficial for MATLAB programmers, students learning matrix operations, and anyone looking to automate matrix creation in MATLAB for numerical analysis or simulations.

glyvin
Messages
4
Reaction score
0
Hi, I'm trying to write an SOR program in Matlab and have everything done, except I cannot figure out how to create a matrix of the following form without manually typing everything in:
x=(1,0,...,0,1)T for various sized matrices.

Thank you for your help.
 
Physics news on Phys.org
What is x equal to? Is it x=(1,0,1,0,...,1,0) or x=(1,0,0,1,1,0,0,1,...,0,1)

In the first case, the expression "1,0" appears over and over again
In the second case, the expression "1,0,0,1" appears over and over again

Here is an idea for the first case:

n=10 <-- any number
x=zeros(n,1);
for i=1:n
if(mod(i,2)!=0)
x(i)=1;
endfor
 
I think glyvin wants a set of zeros inside a a vector with only ones at each end.

This can be done fairly simply with something like
x=zeros(n,1);
x(1)=1; x(end)=1;
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K