How can I write such a large matrix?

thank you very much, how can I construct the matrix b, b: no. of column=20 and
no. of rows=2^20=1048576.
the first column=[0 1 0 1 0 1 ........]
the second column=[0 0 1 1 0 0 1 1 0 0 1 1 ........]
the third column=[0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 ..........]
the fourth column=[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1..................]
and so on. instead of the following command
for j=1:20
for k=0:(2^(ns-j))-1
a((2^(j-1))+1+k*(2^j):(2^j)+k*(2^j))=1;
end
b(j,:)=a;
clear a
end
b=b';
to take less time compared with the commands I use

2 件のコメント

Stephen23
Stephen23 2015 年 1 月 11 日
This is unreadable. Please help the other people here by formatting your text correctly. You should edit your question and use the {} Code button above the text box.
You should also read these:
Oleg Komarov
Oleg Komarov 2015 年 1 月 11 日
@esraa: how can you fill a 1.5L bottle with 10L? The answer is you can't. If you keep asking the same question we won't be able to help. You need to tell us what do you need those 10L to begin with, so maybe we can start filling the bottle with the first 1.5L, use it, empty the bottle, re-fill and repeat until we used all 10L.
This is block processing. You need to split your problem in subproblems, in blocks whose results can then be combined at the end.
Please reformulate you question keeping specifically my suggestion in mind.

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

 採用された回答

Roger Stafford
Roger Stafford 2015 年 1 月 11 日

1 投票

b = zeros(2^20,20);
for k = 1:20
b(:,k) = repmat([zeros(2^(k-1),1);ones(2^(k-1),1)],2^(20-k),1);
end

4 件のコメント

esraa
esraa 2015 年 1 月 11 日
Roger Stafford,thank you, I tried your suggestion and it gives the same results. These commands are a part of m-file, when I try to run this m-file, it takes along time and the results do not appear. these files are attached, if you can help you to recognize the problem and how to solve it . the main file is main_pq.m and it calls datapq.m
Stephen23
Stephen23 2015 年 1 月 11 日
編集済み: Stephen23 2015 年 1 月 11 日
You seem to be asking the same question as here:
There are multiple ways you can avoid creating large matrices, or to speed up calculations. This is a common problem in all of programming, and it not specific to MATLAB. In response to your last question I showed some methods that you need to consider when writing MATLAB code. You can also consider such things as
  • dynamic programming
  • revising the need to create the entire matrix
  • using a GPU for simple operations in parallel
John D'Errico
John D'Errico 2015 年 1 月 11 日
He is asking the same question OVER AND OVER AGAIN. The answer won't change. If the matrix is too big for his computer then get a faster computer. Get more memory. Use 64 bit MATLAB.
Stephen23
Stephen23 2015 年 1 月 11 日
編集済み: Stephen23 2015 年 1 月 11 日
The code could also do with some improvement...

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

その他の回答 (1 件)

Raquel Taijito
Raquel Taijito 2020 年 3 月 29 日

0 投票

How do I create a large matrix of 96 by 96 with each element being 0.6?

1 件のコメント

Walter Roberson
Walter Roberson 2020 年 3 月 29 日
ones(96)*0.6
If I recall correctly, there are approaches that are slightly faster but take more code. The difference in timing is small unless you have to do a lot of this.

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by