How to write a for loop to look at the rows of a M X N matrix?

1 回表示 (過去 30 日間)
Srikanta Sharma
Srikanta Sharma 2012 年 8 月 23 日
i Have a M X N matrix, and I need to divide the rows to n =130 data points and find the max value in that, and then move down the row by (n+1):2n,(2n+1):3n.. until the end, and I need to repeat this iteration for all the columns in the data, and save the data alongside the row number. could you please help me with this. the code which I have written is below:
*newdepth corresponds to length of my data in mm, and the input function asks me to scan the data for a desired depth.
section = input('Size of section (mm): ');
sectionrows = (section*(rsize))/(newdepth);
sectionrows=round(sectionrows);
Row= [];
nsections = (rsize/sectionrows);
nsections =round(nsections);
for k = 1:sectionrows
for i = 1:nsections
Vmax = max(max(hil1(k+(i-1)*sectionrows,:))); % Reflected maximum voltage amplitude from normal tissue
Row=[Row Vmax];
end
end

採用された回答

Honglei Chen
Honglei Chen 2012 年 8 月 23 日
編集済み: Honglei Chen 2012 年 8 月 23 日
It looks like below is a simplified version of what you want to do
a is a matrix with 6 rows and the max is found for every 3 rows
a = rand(6,10);
sz_a = size(a);
n = 3;
max(reshape(max(reshape(a,n,[])),[],sz_a(2)),[],2)
  2 件のコメント
Srikanta Sharma
Srikanta Sharma 2012 年 8 月 24 日
Hi Honglei,
Thank you for your answer, but I seem to have a problem with the code you suggested, I get the following error when I use your code.
my Matrix has 10019 rows and 824 columns. and I want to crawl down 200 rows every time, until the end and find a max value in that and save it as a vector for all the columns. Many thanks in advance.
Error using ==> reshape Product of known dimensions, 300, not divisible into total number of elements, 8255656.
Jan
Jan 2012 年 8 月 24 日
編集済み: Jan 2012 年 8 月 24 日
@Srikanta: Yes, Honglei's version is just a simplified methods, as mentioned. You have to adjust the maximum possible size by your own.

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

その他の回答 (1 件)

Jan
Jan 2012 年 8 月 24 日
編集済み: Jan 2012 年 8 月 24 日
data = rand(10019, 824);
[row, col] = size(data);
len = floor(row / 200);
block = reshape(data(1:len * 200, :), 200, len, col);
result = squeeze(max(block, [], 1));
  1 件のコメント
Srikanta Sharma
Srikanta Sharma 2012 年 8 月 24 日
Thank you Jan and Honglei, it works fine now.

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

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by