create new matrix with special structure

I have a Matrix named p. the sixe of it is m*n (m=6, n=4)
I want to create new matrix based on matrix p with aid of following rule
of course my real Matrix has a large size (500*500*6) and this question is just an example.
I write some thing but it must repeat in columns with so called rule and also I must save it in special structure
and my simple code:
for j=0:2:m-1
newmatrix=[p(1+j,1) p(2+j,1)]

 Accepted Answer

Stephen23
Stephen23 on 11 Jan 2015
Edited: Stephen23 on 11 Jan 2015
Try this:
>> A = reshape(1:6*4,6,4); % define a fake data array of size 6x4
>> B = struct('data',reshape(num2cell(reshape(A(1:end-6),2,[]).',2),3,[]))
B =
3x3 struct array with fields:
data
>> B(1).data
ans =
1 2
>> B(9).data
ans =
17 18

2 Comments

Thank you, Thank you very much
It works exactly.
But is there any way that I can enter the last column od matrix A in computation?
Thank You I got it, Thank you very much

Sign in to comment.

More Answers (2)

Yona
Yona on 11 Jan 2015
Edited: Yona on 11 Jan 2015
The simple way:
for j=0:2:m-1
for i=1:n
newmatrix(1+j,2*i-1:2*i)=p(j+[1 2],1);
end
end

3 Comments

Thank you but it isn't true
Imagine that my matrix is
p1 =
6.0000 10.0000 2.0000 20.0000
6.6667 7.0000 12.0000 4.0000
1.3333 2.0000 20.0000 6.6667
0.6667 0.4000 2.0000 0.2000
0.6000 0.5714 0.3333 1.0000
3.0000 2.0000 0.2000 0.6000
your code give me
newmatrix =
6.0000 6.6667 6.0000 6.6667 6.0000 6.6667 6.0000 6.6667
0 0 0 0 0 0 0 0
1.3333 0.6667 1.3333 0.6667 1.3333 0.6667 1.3333 0.6667
0 0 0 0 0 0 0 0
0.6000 3.0000 0.6000 3.0000 0.6000 3.0000 0.6000 3.0000
it apply just on column 1 but the other column have their own rules.
also I have special structure I mean when I call cell(2,1)from new matrix I get two value p(3,1) and p(4,1)
Or about cell(2,3)I get two value: p(3,3) and p(4,3)
Thank you
to remove the zeros you need to replace
newmatrix(1+j,2*i-1:2*i)
by
newmatrix(1+j/2,2*i-1:2*i)
but in not change the stracture. for this you have the first answer.

Sign in to comment.

Stephen23
Stephen23 on 11 Jan 2015
Edited: Stephen23 on 11 Jan 2015
Try this:
>> A = reshape(1:6*4,6,4); % define a fake data array of size 6x4
>> reshape([reshape(A(1:2:end-6),3,[]);reshape(A(2:2:end-6),3,[])],3,[])
ans =
1 2 7 8 13 14
3 4 9 10 15 16
5 6 11 12 17 18
This matches exactly with the sample output in your question.
Note that numel(output)/2 must be divisible by size(output,1) (in this example 18/2 = 9, which is divisible by 3 rows).

6 Comments

Thank you but I want the STRUCT, I need structure like my figure in which each cell has two value. after that when I call each cell I get two value.
Stephen23
Stephen23 on 11 Jan 2015
Edited: Stephen23 on 11 Jan 2015
Oh, I thought you really did mean a 'matrix'. This is a much easier problem to solve... see my new answer soon!
something like that
empty_mat.x=[]
mat=repmat(empty_mat,3,2)
m=6
m=4
f=0:n
f=repmat(f,n,1)'
for k=1:12
mat(k).x=[ ]
for j=0:2:m-1
for I=1:n
a=f(I,:)
b=[p(1+j,1+a) p(2+j,1+a)] mat(k).x=[mat(k).x x]
but it doesn't work
Doing this using nested loops is not good MATLAB code practice. See my other answer for a neater way to achieve this.
Excuse me, you pointed that using of loop is not good but, Is it possible to give me even some clue that I revise the way using loop?
The main way to reduce the number of loops in MATLAB is to use a feature of many operations and functions that can operate on complete arrays in one go without looping. This is known as vectorization . All basic arithmetic operations can operate on complete arrays, as can many, many functions. You will need to read the documentation carefully for a function to know if this is the case, and how to use it. Luckily the documentation always has good examples too!
Often vectorized code is (sometimes quite a lot) faster then loops, and it can also look a lot more like their equivalent mathematical operations than they would in other programming languages.
For example, instead of looping over elements in an array and performing some operation, you can use one of the several different indexing options to select all array elements that match some criteria, process these simultaneously and then return the results.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 11 Jan 2015

Commented:

on 11 Jan 2015

Community Treasure Hunt

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

Start Hunting!