How to do an array conversion into a different size of array

3 views (last 30 days)
Hello, I would like to try to make an array into a different size of array.
If I assume my array one is 1x16 array, and my desired one is 1x100 array at desired rows, how can I translate it?
For example, I can achieve it manually as decribed below (W - exsist array, WD desired array):
WD(1,34) = W(1,1);
WD(1,35) = W(1,2);
WD(1,36) = W(1,3);
WD(1,37) = W(1,4);
WD(1,44) = W(1,5);
WD(1,45) = W(1,6);
WD(1,46) = W(1,7);
WD(1,47) = W(1,8);
WD(1,54) = W(1,9);
WD(1,55) = W(1,10);
WD(1,56) = W(1,11);
WD(1,57) = W(1,12);
WD(1,64) = W(1,13);
WD(1,65) = W(1,14);
WD(1,66) = W(1,15);
WD(1,67) = W(1,16);
other rows of WD is 0.
However, when I try to do it by using for loops, it only reads the last value of W(1,16) onto the desired points on WD.
This is my for loops.
for i = 30:10:60
for j = 4:7
for k = 1:16
WD(1,i+j) = W(1,k);
end
for l = 68:100
WD(1,l) = 0;
end
end
end
I believe I have to modify the loop status somehow, but I found it difficult at the moment.
How can I fix it?
Any help would be much appreciated.
Thank you.
  2 Comments
Guillaume
Guillaume on 1 Mar 2019
I forgot I was going to comment about your code to explain a few of the problems. In general, you should strive to avoid loops. Certainly copying elements from one array to another should never require a loop. Just use indexing.
Problems with your loops:
for k = 1:16
WD(1,i+j) = W(1,k);
end
The destination here is the same for each step of the loop. So you're copying 16 different values into the same index, each time overwriting the previous one you've copied. Of course, that doesn't work. At the end, WD(1, i+j) is always W(1, 16).
Note that when using vectors, you should use linear indexing (ie W(16)) instead of 2d indexing. With 2d indexing you're forcing the vector to be in particular direction (in your case a row vector). With linear indexing, the code works regardless of the shape (row, column, whatever).
for l = 68:100
WD(1,l) = 0;
end
That can be simplified to
WD(1, 68:100) = 0; %no need for a loop when you can just pass all the indices at once.
%or
WD(68:100) = 0;
Dan Park
Dan Park on 1 Mar 2019
Thank you for your help Guillaume!
You are giving me a very good advice beyond the problem. I must learn how to make the code compact.
'Certainly copying elements from one array to another should never require a loop. Just use indexing.'
Yes, I realised that I don't need any for loops for this problem. You've pointed out what I was exactly encountered, and now I see where my problem comes from. I will try fix it with much simpler codes.
Thank you again.
Dan

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 1 Mar 2019
Even simpler, no loop needed:
starts = [34, 44, 54, 64];
span = 4;
WD = zeros(1, 100);
WD(starts + (0:span-1)') = W(1:numel(starts)*span); %requires R2016b or later, or bsxfun for the +
  5 Comments
Dan Park
Dan Park on 1 Mar 2019
Thank you for your help again, Stephan.
I will take that into account in my code.
Dan
Guillaume
Guillaume on 1 Mar 2019
If you tell us more about your actual use case, we can give you a more generalised code.
But certainly, if you can generate the list of indices with simple math operations (and automatic array expansion as I've done), it will be a lot simpler. Triple loops are certainly completely unnecessary and you should learn to avoid them.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!