Hello I have a matrix as shown in attachment here. Is it possible to write such a program that it will count it elements from left to right but not from top to bottom. Example can I write it as
[0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0]
instead of writing it as
[0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

 Accepted Answer

Stephen23
Stephen23 on 23 Mar 2017
Edited: Stephen23 on 23 Mar 2017
>> vec = [0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0];
>> mat = reshape(vec,4,[]).'
mat =
0 0 0 0
0 0 0 0
0 0 0 0
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 1 0 0
And to convert back again:
>> reshape(mat.',1,[])
ans =
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

3 Comments

Hi I tried this way previously but i didnt get what i actually want. Thank you!
Stephen23
Stephen23 on 23 Mar 2017
Edited: Stephen23 on 23 Mar 2017
@kritika adhikari: Does my answer resolve your question? If it does not then please clarify with more details.
As far as I understand, kritika is satisfied with your answer, but tried something similar before, which must have included another mistake.
@kritika: Is it working now? Then please accept this answer. If not, explain the problems. Thanks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!