cell and matrix and for roof function

1 回表示 (過去 30 日間)
Jeil Park
Jeil Park 2019 年 5 月 16 日
コメント済み: Guillaume 2019 年 5 月 17 日
clear; clc;
N=6;
Ncell_1=11; Ncell_2=21; Ncell_3=31; Ncell_4=41; Ncell_5=51; Ncell_6=61;
Ncell=[Ncell_1,Ncell_2,Ncell_3,Ncell_4,Ncell_5,Ncell_6];
for i=1:N
for j=1:N
for m=1:Ncell(i)
for n=1:Ncell(j)
A(m,n)=zeros;
end
end
B{i,j}=[A];
end
end
my desired answer is that in the each element in the B cell is symetric like the following.
B
[11*11 doubles, 11*21 doules, 11*31 doubles, 11*41 doubles, 11*51 doubles, 11*61 doubles]
[21*11 doubles, 21*21 doubles, 21*31 doubles, ...............................................21*61 doubles]
[31.............................................,31*31doubles,......................................................
[41...
[51...
[61*11doubles,....................................................................................................61*61 doubles]
But there is the problem in my code to get the desired answer. but i can't find it.

回答 (2 件)

Guillaume
Guillaume 2019 年 5 月 16 日
First, what is the point of:
Ncell_1=11; Ncell_2=21; Ncell_3=31; Ncell_4=41; Ncell_5=51; Ncell_6=61;
Ncell=[Ncell_1,Ncell_2,Ncell_3,Ncell_4,Ncell_5,Ncell_6];
when you could have just written:
Ncell = [11, 21, 31, 41, 51, 61];
A lot less typing, a lot easier to read and doesn't use numbered variables (If you start numbering variables, you're doing it wrong!).
You could also have written:
Ncell = 11:10:61;
Also, we're not going to use N. Don't hardcode the size of arrays. If tomorrow you decide you want 7 elements, you have to make sure you change the Ncell array and the N scalar. If one doesn't match the other, your program won't behave correctly. So, instead simply tell matlab to use the number of elements in Ncell whatever that is.
Finally, you don't seem to understand how to construct a matrix of zeros. To construct a matrix of size m x n you don't use loops, you simply use:
A = zeros(m, n);
So, to summarise, the correct code if you were to use loops would be:
Ncell = [11, 21, 31, 41, 51, 61];
B = cell(numel(Ncell), numel(Ncell)); %always preallocate arrays before loops
for row = 1:numel(Ncell)
for col = 1:numel(Ncell)
B{row, col} = zeros(Ncell(row), Ncell(col));
end
end
However, a much simpler way to get what you want would be:
Ncell = [11, 21, 31, 41, 51, 61];
B = mat2cell(zeros(sum(Ncell)), Ncell, Ncell); %create a square matrix of size sum(Ncell) with zeros, then split into cell array according to Ncell with mat2cell
No loop required.
  2 件のコメント
Jeil Park
Jeil Park 2019 年 5 月 16 日
Thank you so much.
But the above code is that I simplified my real code.
I really want to use four [for roof function] so that my real code works.
Could you let me know the problem in my code?
Guillaume
Guillaume 2019 年 5 月 16 日
The main issue with your code is the lack of preallocation. At i=1, j=6, you've grown your A to a 11x61 matrix. At i=2, j=6, since you've not changed A, it's still a 11x61 matrix. You're first filling the first 11x11 elements, then growing it to 21 rows, so end up with a 21x61 matrix.
With the 4 loops, the correct code would be:
B = cells(numel(Ncell), numel(Ncell)); %preallocation of cell array. Not so critical but will speed up the code:
for i = 1:numel(Ncell)
for j = 1:nueml(Ncell)
A = zeros(Ncell(i), Ncell(j)); %preallocation of A, of the correct size. Critical!
for m = 1:Ncell(i)
for n = 1:Ncell(j)
A(m, n) = something;
end
end
B{i, j} = A;
end
end
However, if you explain what you truly want to do, we can probably find a way to do it without loops (which would be more efficient).

サインインしてコメントする。


Jeil Park
Jeil Park 2019 年 5 月 17 日
Honestly, It is first time for me to use Matlab Answers.
It really works and is helpful. Thank you so much.
You save my day !
the response is so fast. It is really good.
Thanks a lot again.
  3 件のコメント
Jeil Park
Jeil Park 2019 年 5 月 17 日
I don't know how to accept it since it is first time.
Could you tell me how to accept? the answer helped me solve my problem a lot! Thanks
Guillaume
Guillaume 2019 年 5 月 17 日
Click on the green accept button next to the answer you want to accept.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by