필터 지우기
필터 지우기

How to use loop to distribute values from one matrix to another matrix in a certain order?

조회 수: 2 (최근 30일)
For example, let's say I have a 3x2 matrix called A and a 3x2 matrix called B. I want the first number in A (1st row, 1st column) to go to the first number in B (1st row, 1st column). However, I want the second number in A (1st row, 2nd column) to go to the 2nd row 1st column of B. I want the third number of A (2nd row, 1st column) to go to the 3rd row 1st column of B.
Is there a single code I can use or would I have to use multiple lines of codes? I know I can probably use loop or for loop, but how exactly?
(Visually it should look something like this)
A = 2 5
8 4
5 6
B = 2 4
5 5
8 6

채택된 답변

Image Analyst
Image Analyst 2016년 2월 16일
Try this:
B = reshape(A(:)', [], 2)
  댓글 수: 2
lauuser1
lauuser1 2016년 2월 16일
That works exactly how I want it to, however is there any way to incorporate the loop function? Would it be any different if A was a different size than B?
Image Analyst
Image Analyst 2016년 2월 16일
I guess you could use for loops if you want.
You can specify the number of columns for 2 as the last argument. It does not need to be the same as A but the total number of elements in B must be the same as A of course.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by