Problem to create a matrix

조회 수: 2 (최근 30일)
Avelino
Avelino 2011년 10월 28일
Hello,
I am trying to create a matrix in Matlab e.g., A with 300 lines and 780 columns. I have also one other one, that is just one column B with 780 lines. I wanted to create a matrix A that goes in 0.02, i.e., 0; 0.02 (...) to B(1). This in the first line. In the second one something like A(780) + 0.02 and further till i=300. My approach in matlab was the following:
for i=1:300 for j=780 A(i,j) = B(i):0.02:B(i+1) end end
With this, MatLab is presenting me an error of the dimensions: ??? Subscripted assignment dimension mismatch.
If somebody could help me, I really would appreciate :)
Thank you!
  댓글 수: 2
Daniel Shub
Daniel Shub 2011년 10월 28일
It is unclear what you want your matrix to look like. Can you edit the question to include a few more "lines" (these are often called "rows") of the desired matrix. From your example it is unclear why matrix A would have 780 columns. It is possible that 0, 0.02, ..., B(1) would have 780 elements, but in general it will not. Further, A(780)+0.02 will definitely not have 780 elements.
Avelino
Avelino 2011년 10월 28일
Hello Daniel,
Thank you. Now I just realize that I make mistake, the Matrix B have 300 lines.
It should look something like this, let's say that:
B =
16
32
48
in this case A should look like
A =
0 0.02 (...) 16 (with 800 columns)
16 16.02 (...) 32 (with 800 columns)
32 32.02 (...) 48 (with 800 columns)
This example is slightly different from mine as I have 780, but the method is the same.
Thank you very much,
Avelino

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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 10월 28일
B = (1:300)'*16;
A = bsxfun(@plus,B-16,0:.02:B(1));
  댓글 수: 1
Daniel Shub
Daniel Shub 2011년 10월 28일
I always forget about bsxfun. I have only recently started to remember about arrayfun.

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

추가 답변 (1개)

Daniel Shub
Daniel Shub 2011년 10월 28일
Does this do what you want?
B = (1:300)*16;
A = cell2mat(arrayfun(@(x)plus(x, 0:0.02:(16-0.02)), B, 'UniformOutput', false)')-16;

카테고리

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