MATLAB producing ans but wont upload variable, extremely strange behavior

I am doing nothing special here but vectorizing an array
When i do something like the below code below matlab is going off, and producing an "ans" on the screen that has the correct values in it.
However, it is creating a completely empty variable called "test" despite the fact that i am setting that command to the variable test.
So i get the correct "ans" on the screen and an empty variable called test
What is going on here? I can click on "ans" in the workspace and see everything that should be there but test will be a variable of the same size but empty.
test = test1(1,1);test2(1,2)

 Akzeptierte Antwort

Image Analyst
Image Analyst am 31 Jul. 2021
Bearbeitet: Image Analyst am 31 Jul. 2021
Did you forget to enclose the vectors in brackets to form a matrix. See this:
test1 = rand(1,2);
test2 = rand(1,2);
test = test1(1,1); test2(1,2) % Two separate lines of code on the same line
test = [test1(1,1); test2(1,2)] % one line of code to stack the vectors on top of each other in a new matrix.
Did that produce a forehead-slapping moment?

14 Kommentare

no it did not produce a forhead slapping moment because when i tried backets it gives me the wrong data back.
When i use brackets it goes me an additional row in the matrix which should not be there in any way shape or form
I am expecting a single row vector back
i am trying to concat the data from two things horizontally next to each other and leave it in the same place
So maybe the ; is not the correct operator. I understand this concats things vertically
So how do i get it to concat horizontally, which is exactly what "ans" gives me on the screen. I want exactly what ans has in it but it will not put it in the variable i am telling it to.
How is it possible that it will put it in ans correctly as i want but not in my variable
Also, putting a coma instead of a ; came back with exactly what i want also but again will not assign it to my varible
"i am trying to concat the data from two things horizontally next to each other" <== then you need brackets, not no-brackets like you had.
This should work
test = [test1(1,1), test2(1,2)] % Two elements side-by-side.
test = [test1(:).', test2(:).'] % ALL elements side-by-side.
Did either of those do what you want?
So the first line you have me worked to some extent
I tried a comma but what it returns is a 2 row cell array
The first row is empty for some reason
and the second row has the data as it should be.
?
What is the size of the test arrays? 1-by-2? 2-by-2? Please give a small example with actual matrices and show what you want as the output.
ok so i think i figured out what was going on
So in your example of on taking the two vectors and concating them together
i see you are essentially using like a "dot" operator
aka element wise
And that is essentially what im after
There is a distinction to be made between concating the two entire vectors together where it just places 1 next to the other versus placing element 1 from array one next to element 1 of array 2 which is what im looking for
So i applied your dot operator as you have it, and i get back a 2 col vector with element 1 from array 1 in location row element 1 from array 2 in row 1 col 1
What i need is for them to be concated together.
So getting warmer but concated together.
What im looking for is a row vector with element 1 from array 1 and element 1 from array 2 side by side in a row vector concated together. I am down this path because of performance problems so i am weary of using anything like strcat or join
I'm trying to work with you here but you didn't give an example, so I made one and hope one of these cases is what you want:
% Case 1 : different lengths.
test1 = [1,2,3,4]
test2 = [21,22,23,24]
output = reshape([test1;test2], 1, [])
% Case 2 : different lengths.
test1 = [1,2,3,4]
test2 = [20,21,22,23,24,25] % Different length for generality.
L1 = length(test1);
L2 = length(test2);
output = []
for k = 1 : max([L1, L2])
if k <= L1
output = [output, test1(k)]
end
if k <= L2
output = [output, test2(k)]
end
end
Robert Scott
Robert Scott am 31 Jul. 2021
Bearbeitet: Robert Scott am 31 Jul. 2021
absolutley cant use for loops
That is what i meant by vectorizing. The whole point of this is to get rid of a for loop i had because matlab is so rediculously slow. Thank you for your continued efforts
Robert, it is not true that MATLAB's for loops are so ridiculously slow. That is a stubborn myth held over from the 80's and 90's that just won't go away. In fact I can show cases where for loops are faster than vectorized solutions (see attached demo).
You'll note that the first case I made up was a fully vectorized case. But since you didn't accept the answer, and complained about the for loop solution, I'm left to assume that you have two different size vectors. So I modify case to to be full vectorized:
% Case 2 : different lengths.
test1 = [1,2,3,4]
test2 = [20,21,22,23,24,25] % Different length for generality.
L1 = length(test1);
L2 = length(test2);
% Pad if necessary
if L1 < L2
t1 = [test1, zeros(1, abs(L2-L1))]
t2 = test2
elseif L2 < L1
t1 = test1
t2 = [test2, zeros(1, abs(L2-L1))]
else
t1 = test1;
t2 = test2;
end
output = reshape([t1;t2], 1, [])
Note that the zeros are interleaved with the longer end of the vector, it's not just the longer vector only there like I showed with the for loop.
Not sure why after multiple requests you're still not giving a small example with inputs and desired outputs. I'll help you if you do, but if you don't, I think I've helped enough for you to figure it out on your own.
i do apprecaite your help
I am unable to share code for IP reasons. Although i do understand it would be useful.
So i was able to complete what i needed to do with a series of silliness.
From your example
test = [test1(:).', test2(:).']
test = join(test)
strrep(test,' ','');
Which is stupid but it works
The for loop speed i can assure you is a major issue. I am working with very very large files. gigs of data. With for loops it takes hours and hours for my code to run.
I redid it using parfor loops whihc gave me about a 4X speed enhancment however it crashes all the time and locks up my computer so i gave up on parfor.
I do thank you for your help. I would love to see some example where a for loop is faster then vectorized format.
Well I work with IP and patents all the time and they wouldn't care if I just gave some made up data with simple, made-up numbers. But whatever, glad join works in the way you want.
I'm surprised that when you ran my demo had the vectorized way winout every single time, and didn't see any runs where the for loop was faster. That would be an extremely rare event. When I run it I get:
--------------------------------------------------
Vectorized won 71 times out of 1000 = 7.1%.
For loop won 929 times out of 1000 = 92.90%.
The average time for the vectorized method = 0.000164023.
The average time for the for loop method = 0.000107099.
So for you to have the vectorized way win all 1000 times would be astonishing to say the least.
not sure , but what i can tell you is that this is part of a function that is called nearly a million times
And going vectorized just save me 20 mS per call
I was running it with join() to see how that differed from my two solutions and it appears that your input vectors area actually strings, not numerical, because join() only works with text.
So for completeness, I'm showing the two cases with different length vectors.
% Declare simple sample data.
test1 = [1,2,3,4];
test2 = [21,22,23,24,25,];
% For case 3, to use strrep, they must be strings, not numbers.
test1 = ["1","2","3","4"];
test2 = ["21","22","23","24", "25"];
% Case 2 : different lengths.
L1 = length(test1);
L2 = length(test2);
% Pad if necessary
if L1 < L2
t1 = [test1, zeros(1, abs(L2-L1))]
t2 = test2
elseif L2 < L1
t1 = test1
t2 = [test2, zeros(1, abs(L2-L1))]
else
t1 = test1;
t2 = test2;
end
output2 = reshape([t1;t2], 1, [])
% Case 3: join - requires strings, not numbers.
% Create an array of 8 elements
test = [test1(:).', test2(:).']
% Make it a single string with spaces between, not an array.
test = join(test)
% Remove all spaces.
test = strrep(test,' ','')
You see:
output2 =
1×10 string array
"1" "21" "2" "22" "3" "23" "4" "24" "0" "25"
test =
1×9 string array
"1" "2" "3" "4" "21" "22" "23" "24" "25"
test =
"1 2 3 4 21 22 23 24 25"
test =
"12342122232425"
If I had known that you were dealing with text in advance, and if I had known what you wanted in advance, it would have gone much quicker with less back and forth. Note that I used simple data for the example that was not proprietary or secret so I would not get into trouble with my company over IP issues because of patents I'm working on.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by