How can I merge 3 matrices into one and skip some variables?

1 次查看(过去 30 天)
I have 3 matrices(size 178x1) and I want to merge them into 1 matrix BUT I want to skip rows numbered 48 to 59 and 108 to 130. Seems my code isn't doing the work.
for i=1:178
if i==49
i=60
end
if i==108
i=131
end
Wines=[TotalPhelons(:,i), Flavanoids(:,i), NonflavanoidPhenols(:,i)];
end
What I get is a matrix 178x3. And I get this error :
Index exceeds matrix dimensions. Error in ThirdStep (line 15) Wines=[TotalPhelons(:,i), Flavanoids(:,i), NonflavanoidPhenols(:,i)];
EDIT: I want the new matrix to have 3 collumns (each collumn for each matrix)
  1 个评论
Stephen23
Stephen23 2017-1-16
编辑:Stephen23 2017-1-16
It is not possible to use that syntax to "skip" loop iterations. If you wish to use a for loop and "skip" iterations then you need to specify this as the for loop input:
for k = [1:47,60:107,131:178]
...
end
Note that this will still not give you what you want, because the output matrix will be indexed at the same rows as the input matrix, and so will be the same size (it will fill the intermediate rows with zeros). You could loop over a separate vector of indices... but really the best solution is to forget about ugly loops and learn to write efficient MATLAB code without loops.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2017-1-16
编辑:Stephen23 2017-1-16
Doing this in a loop is very inefficient. You should do this all at once using indexing:
idx = [1:47,60:107,131:178];
out = [TotalPhelons(idx), Flavanoids(idx), NonflavanoidPhenols(idx)]
  2 个评论
Iraklis S
Iraklis S 2017-1-16
I still get an Index exceeds matrix dimensions. error even though I end up with a 178x3 out matrix. Shouldn't I get a matrix with size 144x3??

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by