Trouble replacing one matrix values with vector values.

Good Morning,
I have a simple problem again, if anybody has any thoughts.
I keep getting an error in this bit of code but I've looked to see that the matrix and vectors are the same size, so i'm stuck as to how to get around it.
for i = 1:183
Day2(Day2(:,i) == 1) = D([1,i])
end In an assignment A(:) = B, the number of elements in A and B must be the same.
This is the error, but Day2 is 7 x 183 double and D is 1 x 183 double. I have tried to just pass one row of Day2 through at a time. When I can get it to run without the error I get returned back Day2 but it has not had any modifications the values from D I want to fit in have not been.
Any help or advice would be appreciated. I've been reading over all the indexing information but perhaps I'm missing what I should be looking at.
Thank you very much for any time, help and advice!

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 10 月 13 日

0 投票

D([1,i]) refers to two locations. D(1,i) refer to a single location

5 件のコメント

Chameleon17
Chameleon17 2015 年 10 月 13 日
Ah yes, sorry, I had changed that, but when I do that it gives me my original matrix back with none of the values changed to those in the D vector?
Stephen23
Stephen23 2015 年 10 月 13 日
編集済み: Stephen23 2015 年 10 月 13 日
Day2(Day2(:,i)==1) = D([1,i])
The RHS D([1,i]) refers to exactly two elements using linear indexing, whereas Day2(:,i)==1 could create any number of true values, from 0 to 183, depending on the values of Day2, so the Day2(Day2(:,i)==1) could refer to any number of elements (using logical indexing).
Your code likely refers to two elements on the RHS, and some other (unknown) number of elements on the LHS, thus the error.
Chameleon17
Chameleon17 2015 年 10 月 13 日
Thank you both very much for your answers! I always learn the most from these answers. I managed to sort it out on my own using a second for loop. I'm not sure if that's considered messy, I'm still learning what messy code is, but yes, thank you for the explanation as to why I was getting the error, it helps a lot!
Stephen23
Stephen23 2015 年 10 月 13 日
What are you actually wanting to do? If you explain the goal of this then we can show you how it can be done neatly and efficiently. Loops are probably not an efficient solution.
Chameleon17
Chameleon17 2015 年 10 月 13 日
Well, I've got matrices all the same length (183) but varying number of rows, for multiple areas across the UK, each row representing a different location within each area, each of the 183 columns is a day. I have a vector of dates (1x183) which I would like to input the value of each date into the matrix when I have presence of disease.
I've done it as such:
for n = 1:7
for i = 1:183
if Day3(n,i) == 1
Day3(n,i) = D(1,i)
Period = Day3
end
end
end
Is this considered messy?
I know I have to repeat it for each matrix, but I think I want to analyze everything separately so that is okay.

この質問は閉じられています。

質問済み:

2015 年 10 月 13 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by