0

I have one index vector with nine elements,IDX = [1 2 1 3 4 3 1 2 1].(in another post, I asked about how to repeat two separate vectors IDX1 and IDX2, but here I have all the elements in one vector)

The first five elements of this matrix are to be repeated according to elements in howMany1 = [3 2 2 1 2], the next four elements are to be repeated according to howMany2 = [2 1 2 2]. So the result would be: a1 =[1 1 1 2 2 1 1 3 4 4] and a2 = [3 3 1 2 2 1 1 ]. How could I do it by MATLAB such that the result is stored in two separate matrices in a cell? Here is my code, but I can not find out what the problem is. Thank you all for taking the time.

clear all
close all
clc

IDX = [1 2 1 3 4 3 1 2 1];
howMany1 = [3 2 2 1 2]; % howMany1 and howMany2 determine how many times each index in IDX should be repeated.
howMany2 = [2 1 2 2];

%%
howMany = {howMany1 howMany2};
a = cell(2,1)
m = 1
for k = 1:2
 for j=m:length(howMany)+m-1
     h = howMany{k};
     idx=IDX(j);
     a{k,1}= cell2mat(arrayfun(@(x,y) repmat(x,1,y),idx,h(j),'uniform',0))
 end
 m = m+length(h)
end
KianStar
  • 177
  • 1
  • 9
  • No Daniel, its not repeated, my problem here is totally different from what I asked before. Thank you – KianStar Jan 19 '15 at 10:42
  • 1
    It is apparently not clear what the difference is between the two. I can't see the differences other than some variable names. If you do not think this is a duplicate, please modify your question. – kkuilla Jan 19 '15 at 10:44
  • Here I have all indexes together in a matrix. I don't have them separated as the previous question. This is where Im stuck: I don't know how to split IDX . – KianStar Jan 19 '15 at 10:46
  • I just know that IDX is to be separated according to the length of howMany1 and howMany2. – KianStar Jan 19 '15 at 10:48
  • I still believe this is a duplicate even with after your comment. You may edit the question, link to the other question and explain **thoroughly**, and in a way that makes it obvious to other people what the difference is, and where you're stuck. – Stewie Griffin Jan 19 '15 at 10:54
  • 2
    Split IDX using `idx1 = IDX(1:numel(howMany1))` and `idx2 = IDX(numel(howMany1)+1:end)` and look up the colon operator in the docs. – Thomas Jan 19 '15 at 10:57
  • Thank you so much, Tomas! The problem is solved, great! You made my day! Wish you a nice day! :) – KianStar Jan 19 '15 at 11:27

0 Answers0